How are char * (* arr) [2] and char ** array [2] different from each other?

how are char * (*arr)[2] and char **array[2] different from each other? if I pass char* strings[2] using a function, then how do I access the elements from both of the ways mentioned in the first part of the question? Also talk about other ways to access the elements of an array of pointers. Thanks.

+4
source share
5 answers

Follow the spiral rule : -

a] char * (* arr) [2]

  +------+ | +--+ | | | | | | ^ | | char * (*arr ) [2] | | | | | | | | | +----+ | +---------+ identifier arr is pointer to array of 2 pointer to char 

b] char ** arr [2]

  +----------+ | +----+ | | ^ | | char* *arr [2] | | | | | | +------+ | +------------+ identifier arr is an array 2 of pointers to pointer to char 

Similarly

c] char * strings [2]

  +-----+ | | ^ | char *strings [2] | | +--------+ identifier string is an array of 2 pointers to char 

So, know what differences exist

+7
source

CDecl reports:

 char *(*arr)[2] declare arr as pointer to array 2 of pointer to char 

and

 char **arr[2] declare arr as array 2 of pointer to pointer to char 

It's just that the array declarator [] takes precedence over the * pointer specifier, so the brackets change the value.

+12
source

char * (*arr)[2] - a pointer to an array of pointers.

char **array[2] is an array of pointers to pointers.

if I pass char* strings[2] using a function, then how do I access the elements from both of the ways mentioned in the first part of the question?

strings is an array of pointers, so &strings gives the first type. You cannot get the second type.

In C ++, I would recommend that you stop messing with weird composite types and use higher level classes like std::vector and std::string instead.

+5
source

char **array[2] is a 2-element array of pointers to pointers to a character.

char *(*arr)[2] is a pointer to an array of two elements of pointers to a character.

pi character pointers can be used as a string in C when null terminates.

+2
source

The rule that I have always found useful is โ€œinside out, from right to leftโ€:

 char *(*arr)[2]; 

So, inside the paren group first has "* arr", from right to left, "arr is a pointer"

nothing remained in parens, so "arr is a pointer" to the rest, from right to left: an array of two pointers to char.

A similarly short rule, which has always been useful, is to say that "the value of the C array is a pointer to its first element", which also emphasizes the difference between using the name for its value and for other attributes: f(arr) and arr+1 use the value arr , and its value in each of them is a pointer to its first element (with its delimiter and restrictions), but, for example, sizeof arr does not evaluate and therefore gives the size of the array.

+1
source

Source: https://habr.com/ru/post/1500160/


All Articles