简体   繁体   English

如何使用指向2d char数组的指针?

[英]How can I use a pointer to a 2d char array?

I am trying to use a pointer (*test) with a 2d char array (a[][]). 我正在尝试将指针(* test)与2d char数组(a [] [])结合使用。 But I can not seem to figure out what is the proper way to do it. 但是我似乎无法弄清楚什么是正确的方法。 I managed to get it working with an int array and int pointer, but the same solution did not work when I changed to char. 我设法使它与int数组和int指针一起使用,但是当我更改为char时,相同的解决方案不起作用。 I assume I was just lucky with ints, and did not really have a proper solution. 我以为我很幸运,没有真正合适的解决方案。 I want to use pointer arithmetic to iterate as you can see in the code. 如您在代码中所见,我想使用指针算法进行迭代。 My current output from this is gibberish. 我目前的输出是乱码。

char a[WIDTH][HEIGHT] = { {'a','b'},{'c','d'},{'e','f'} };

char *test = (char *)a[0][0];

int x,y;

for (x = 0; x < WIDTH; x++)
  {
    for (y = 0; y < HEIGHT; y++)
    {
      printf("%c", test);
      test = test + 1;
    }
    printf("\n");
  }
printf("%c", test);

You're trying to print the address of your character, not it's value. 您正在尝试打印角色的地址,而不是它的价值。 Dereference the pointer to access the value: 解引用指针以访问值:

printf("%c", *test);

Moreover, you need to set the pointer to actually point to the address of the first element in your array. 此外,您需要将指针设置为实际上指向数组中第一个元素的地址。 Currently, you're forcing the pointer to misuse it's value as an address: 当前,您正在强迫指针将其值误用作地址:

char *test = (char *)a[0][0];

Change that to 更改为

char *test = &(a[0][0]);

In general, you should compile with all warnings enabled and treat every single one of them as error unless you're absolutely sure that it's not. 通常,除非您完全确定不是警告,否则应该编译所有启用的警告,并将其中的每个警告都视为错误。 You should also try to get by without any casts unless you're absolutely sure that you need one because casts may silence useful compiler warnings. 除非您绝对确定需要使用强制转换,否则还应尝试不使用任何强制转换,因为强制转换可能会使有用的编译器警告无效。

Finally I'd like to add that in my opinion this seems to be a way to iterate over an array that's just asking for future issues. 最后,我想补充一点,在我看来,这似乎是一种迭代只询问未来问题的数组的方法。 If you want to express contiguous memory, use a single (1D) array. 如果要表达连续内存,请使用一个(1D)数组。 If you want grid like access, use a 2D array or even better, provide functions with expressive names. 如果您想要像网格一样的访问,请使用2D数组甚至更好的方法,为函数提供可表达的名称。

Two main problems are 两个主要问题是

  1. You are assigning the value of a[0][0] to test and casting to silence the compiler warning, you should assign the address which is simply a . 您要分配a[0][0]进行test并强制转换以使编译器警告静音,因此您应分配a简单的地址a

     char *test = (char *) a; 

    Or 要么

     char *test = (char *) a[0]; 

    This works because the array values are stored contigously in memory, but note that you should not rely on this if for example you create the 2 dimensional array dynamically, because that requires an array of pointers and hence the values will no longer be stored contigously. 之所以可行,是因为数组值连续存储在内存中,但是请注意,例如,如果您动态创建二维数组,则不应依赖此数组,因为这需要一个指针数组,因此值将不再连续存储。

  2. You are passing a pointer to printf() when you use the "%c" specifier, this specifier expects a char argument instead so it should be 当您使用"%c"说明符时,您正在传递指向printf()的指针,该说明符期望使用char参数,因此应该

     printf("%c", *test); 

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

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