What are the differences between these types of pointers?

I programmed code in C ++ when I accidentally put brackets in my pointer, and the output of my programs changed.

Since I'm new to programming, I wanted to know the difference between these types of pointers:

int* A[n]; int (*A)[n]; int *(A[n]); 

I read in my tutorial that arrays are also types of pointers.

+8
c ++ arrays pointers
source share
2 answers
 int* A[n]; 

First of all, it is an array, regardless of the type of element. After applying the * pointer, we know that A is an array of int pointers.

 int (*A)[n]; 

By using parentheses, the * pointer takes precedence over the [] array in this case. Then A is, first of all, a pointer, regardless of what it points to. After applying the array [], we know that A is a pointer to an int array.

 int *(A[n]); 

The brackets will not change the order of priorities, which will affect the array [], so removing the brackets will cause int* A[n] be the same as your 1st case.

 Are array pointers? 

Not. Array is a data structure that allocates a memory pool and stores data sequentially when the pointer points to a specific index in the memory pool and refers to data stored in this memory cell.

+15
source share

This article contains good examples in reading type declarations in C. http://www.unixwiz.net/techtips/reading-cdecl.html

Basically, you can read types according to the following priority:

  • (Often in parentheses) Type of innermost

  • Highest types (mainly array types: [])

  • The leftmost types, except the outermost (most commonly used pointer types: *)

  • External types (mostly primitive types: int, char ..)

For example, the types you presented can be read as follows:

 int* A[n]; // An array ([n]) of pointer (*) of int. int (*A)[n]; // A pointer (*) of array ([n]) of int. int *(A[n]); // An array ([n]) of pointer (*) of int. 

Thus, the first and third types are identical.

+2
source share

All Articles