The difference between array pointer and pointer array

Prerequisite:
array pointers are pointers array pointers
are arrays

Example:

int *p1[5];

with

int (*p1)[5];

The former is a pointer array and the latter is an array pointer.

Involving the priority of operators: the array subscript [] has a higher priority than the value operator *. Combine from right to left. First combine the array subscript [].

Let's talk about pointer arrays first:

int *p1[5];

Pointer variable pointing to integer data. 0 1 2 3 4 (int *)
**Conclusion: **The pointer array is an array, and each array element stores a pointer variable.
Code example:

#include <stdio.h>
int main()
{
    
    
int a=1;
int b=2;
int c=3;
int d=4;
int e=5;
int *p1[5]={
    
    &a,&b,&c,&d,&e};
int i;

for(i=0;i<5;i++)
{
    
    
printf("%d\n",*p[i]);
}
return 0;
}

This section is roughly similar to the output loop body of the array.

for(i=0;i<5;i++)
{
    
    
printf("%d\n",*p[i]);
}

Just replace the array name with a pointer variable. So the pointer array is still an array.

The pointer array is very convenient to apply to the string (you can use the two-dimensional array)

After the initialization (double quotation marks on the string), output. Note that if the string is output to the address, there is no need to hit *. This value symbol is directly written as p[i]

Array pointer

int (*p1)[5];

The pointer variable p1 points to the entire array, 0 1 2 3 4 (int), and the data type is integer data.

Conclusion: The array pointer is a pointer, it points to an array.

Code example:

#include <stdio.h>
int main()
{
    
    
int temp[5]={
    
    1,2,3,4,5};
int (*p)[5]=&temp;
int i;

for(i=0;i<5;i++)
{
    
    
printf("%d\n",*(*p+i));
}
return 0;
}

Remember not to initialize like this:
int (*p)[5]={1,2,3,4,5}; The
compiler will not report an error but the output content is wrong.

The same is true for array pointers. When defined as char, the output is not added *

Guess you like

Origin blog.csdn.net/yooppa/article/details/112980820