简体   繁体   English

指向2d指针数组的指针

[英]pointer to 2d array of pointers

How would I declare a pointer which points to 2D array of character pointers. 我将如何声明一个指向2D字符指针数组的指针。 Like 喜欢

char *ar[10][10]; 

In my understanding this array is stored as array of array, so ar points to a array of pointers, each of which points to a column in this array. 在我的理解中,该数组存储为数组数组,因此ar指向一个指针数组,每个指针都指向该数组中的一列。 So it has three level of pointers. 因此它具有三个级别的指针。 So it should be declared as 所以应该声明为

char ***p; 

Thus both ar and p are of the same type. 因此, arp属于同一类型。 But if I use p like a 2d array for eg p[0][0] , it gives a segmentation fault. 但是,如果我将p像2d数组那样用于p[0][0] ,则会出现分段错误。 Why does this happen and what would be the correct way to declare p ? 为什么会发生这种情况?声明p的正确方法是什么?

The declaration will look like 声明看起来像

char * ar[10][10]; 

char * ( *p_ar )[10][10] = &ar; 

Dereferencing the pointer for example in the sizeof operator you will get the size of the array because the expression will have type char[10][10] 例如,在sizeof运算符中取消引用指针,您将获得数组的大小,因为表达式的类型为char[10][10]

printf( "%zu\n", sizeof( *p_ar ) );

The output will be equal to 100 输出将等于100

To output for example the first string of the first row of the original array using the pointer you can write 要使用指针输出例如原始数组第一行的第一个字符串,您可以编写

printf( "%s\n", ( *p_ar )[0][0] );

To output the first character of the first string of the first row of the original array using the pointer you can write 要使用指针输出原始数组第一行第一个字符串的第一个字符,您可以编写

printf( "%c\n", ( *p_ar )[0][0][0] );

or 要么

printf( "%c\n", *( *p_ar )[0][0] );

You could also declare a pointer to the first element of the array 您还可以声明一个指向数组第一个元素的指针

char * ar[10][10]; 

char * ( *p_ar )[10] = ar; 

In this case to output the first string of the first row of the original array using the pointer you can write 在这种情况下,使用指针可以输出原始数组第一行的第一个字符串

printf( "%s\n", p_ar[0][0] );

To output the first character of the first string of the first row of the original array using the pointer you can write 要使用指针输出原始数组第一行第一个字符串的第一个字符,您可以编写

printf( "%c\n", p_ar[0][0][0] );

or 要么

printf( "%c\n", *p_ar[0][0] );

Your misconception is that each of the levels would be a pointer. 您的误解是每个级别都是一个指针。 This is not true. 这不是真的。

char ***p means that you have a pointer which points to a pointer which points to a pointer which points to a char. char ***p表示您有一个指向指向char的指针的指针。 But that's not what you have. 但这不是你所拥有的。

Your char[10][10] is stored as 100 chars in a contigouos way, every 10 of them forming a char[10] . 您的char[10][10]以contigouos的方式存储为100个字符,每10个组成一个char[10] So either you indeed use a char *ar[10][10] or you switch to using a char *ar and use that for addressing the array in a 1D fashion. 因此,您确实可以使用char *ar[10][10]或切换到使用char *ar并将其用于以一维方式寻址数组。 This may have advantages, but is generally only recommended if the array's shape is about to be variable. 这可能有优势,但是通常仅在阵列形状即将变化的情况下才建议使用。

Arrays are not pointers and pointers are not arrays. 数组不是指针,指针也不是数组。 Similarly, an array of arrays has absolutely nothing to do with a pointer to pointer. 同样,数组的数组与指针的指针绝对无关。 If someone told you differently they were confused. 如果有人对您的说法有所不同,他们会感到困惑。

char* ar[10][10] is a 2D array of char pointers. char* ar[10][10]是char指针的2D数组。 It can also be regarded as an array of arrays of char pointers. 它也可以看作是char指针数组的数组。

If you want a pointer to such an array, you would use an array pointer : 如果要使用指向此类数组的指针,则可以使用数组指针

char* (*ptr)[10][10]

This is read as 这是作为

  • (*ptr) Array pointer to... (*ptr)指向...的数组指针
  • [10][10] ...a 10x10 array... [10][10] ... 10x10阵列...
  • char* ...of char* items. char* ... char*项目。

The declaration is correct, 声明正确,
p[0][0] is just a pointer to char*,may be you access it before alloc storage to it,so you get a segment fault p[0][0]只是一个指向char *的指针,可能是您在分配存储空间之前对其进行了访问,从而导致段错误

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

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