简体   繁体   English

C中的字符和字符串的二维数组function

[英]Two Dimensional Array of a character and string function in C

I am writing a C program which inputs string from a user sorts it alphabetical Order.我正在编写一个 C 程序,该程序输入来自用户的字符串,按字母顺序对其进行排序。 I have two questions here.我在这里有两个问题。

When I input the strings from user in this way below no error occurs.当我以这种方式从用户输入字符串时,不会发生错误。

for(int i =0; i < 7; i++)
{
    printf("Enter the name %d\n",i+1);
    scanf("%s",&name[i][0]);
}

what does name[i][0] mean if name[i][0] 是什么意思如果

    char name[7][10];

Why are we setting up the 2nd index here to 0,isn't the 2nd dimension the maximum length of string which our arrray can handle.为什么我们在这里设置第二个索引为0,第二个维度不是我们的数组可以处理的最大字符串长度。

My second question is For such type of array what does the string handling function takes &name[i][0] or &name[i] ?我的第二个问题是对于这种类型的数组,处理 function 的字符串需要&name[i][0]还是&name[i]

The code I have written so far gives me desired output however with lots of warning到目前为止我写的代码给了我想要的 output 但是有很多警告

#include<stdio.h>
#include<string.h>

int main() {
  char name[7][10];
  for (int i = 0; i < 7; i++) {
    printf("Enter the name %d\n", i + 1);
    scanf("%s", & name[i]);
    strlwr( & name[i]);
  }
  //Sort in alphabetical order
  for (int i = 0; i < 7; i++) {
    for (int j = i + 1; j < 7; j++) {
      if (strcmp( & name[i][0], & name[j][0]) > 0) {
        char temp[10];
        strcpy(temp, & name[i]);
        strcpy(name[i], & name[j]);
        strcpy(name[j], & temp);
      }
    }
  }
  printf("The names in alphabetical order is\n");
  for (int i = 0; i < 7; i++) {
    puts(name[i]);
  }
}

The warnings are:警告是:

 warning: passing argument 1 of 'strlwr' from incompatible pointer type [-Wincompatible-pointer-types]
           98 |         strlwr(&name[i]);

and similar for other functions as well.其他功能也类似。

names[i] is an element of the array declared like names[i]是声明为数组的元素

char name[7][10];

and has the type char[10] .并且具有类型char[10] That is elements of this two-dimensional array are one-dimensional arrays of the type char[10] .也就是说,这个二维数组的元素是char[10]类型的一维 arrays 。

Used in expression array designators (with rare exceptions as for example used as operands of the sizeof operator) are converted to pointers to their first elements.在表达式中使用的数组指示符(很少有例外,例如用作 sizeof 运算符的操作数)被转换为指向它们的第一个元素的指针。

So the expression names[i] used for example as an argument of the function scanf is converted to pointer to its first element.因此,例如用作 function scanf的参数的表达式names[i]被转换为指向其第一个元素的指针。 That is this expression is equivalent to &names[i][0] .也就是说,这个表达式等价于&names[i][0]

So instead of this call所以代替这个电话

scanf("%s",&name[i][0])

you could write你可以写

scanf("%s", name[i] )

The both expressions used as arguments have the type char * ,用作 arguments 的两个表达式都具有char *类型,

This call of scanf这个 scanf 的调用

scanf("%s", & name[i]);

has an argument of an incorrect type.有一个不正确类型的参数。 The expression &name[i] has the type char ( * )[10] while the function expects an argument of the type char * though the values of the both expressions are equal each other.表达式&name[i]具有char ( * )[10]类型,而 function 需要char *类型的参数,尽管两个表达式的值彼此相等。

If you have an array declared like如果您有一个声明为的数组

char name[7][10];

then the values of these expressions那么这些表达式的值

&name
&name[0] 
&name[0][0]

are equal each other and yields the address of the first byte of the memory extent occupied by the array.彼此相等,并产生数组占用的 memory 范围的第一个字节的地址。

However the types of the expressions are different.但是表达式的类型是不同的。 They are他们是

char ( * )[7][10]
char ( * )[10]
char *

Here is a demonstrative program这是一个演示程序

#include <stdio.h>

int main(void) 
{
    char name[7][10];
    
    printf( "&name       = %p\n", ( void * )&name );
    printf( "&name[0]    = %p\n", ( void * )&name[0] );
    printf( "&name[0][0] = %p\n", ( void * )&name[0][0] );

    return 0;
}

Its output might look like它的 output 可能看起来像

&name       = 0x7ffec1d18e40
&name[0]    = 0x7ffec1d18e40
&name[0][0] = 0x7ffec1d18e40

By the reason described above the arguments of these calls由于上述原因,这些调用的 arguments

    strcpy(temp, & name[i]);
    strcpy(name[i], & name[j]);

also have incorrect type char ( * )[10] while the function expects arguments of the type char * .也有不正确的类型char ( * )[10]而 function 需要 arguments 类型char *

You need to write你需要写

    strcpy(temp, name[i]);
    strcpy(name[i], name[j]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM