A deep understanding of the difference between array pointers and pointer arrays _c language

Source: Internet
Author: User

the difference between an array pointer and an array of pointers is: The array pointer p is a pointer, and the pointer array p is an array that holds n pointer variables.

One, array pointers
int (*p) [n]
emphasis: ()
High priority ([], () The priority is the same, but they are in the direction from left to right, so first run the parentheses in the *pFirst, it shows that P is a pointer to a one-dimensional array of integers, and the length of the one-dimensional array is n, which can be said to be the step of P. That is, when the p+1 is executed, p crosses the length of the N-Integer data (n*sizeof (int)).
To assign a two-dimensional array to a pointer, you should assign a value like this:
int a[3][4];
int (*p) [4];The statement defines an array pointer that points to a one-dimensional array of 4 elements.
p = A;Assigns the first address of the two-dimensional array to p, i.e. a[0] or &a[0][0]
p++;//<=>A[1]<=>P[1]
When used to point to a two-dimensional array, the reference is the same as the array name reference.a<=>p。 For example, to represent an array of I rows J column an element A[i][j]:
P[i][j]<=>a[i][j] <=>* (p[i]+j)<=>* (a[i]+j) <=> * (* (p+i) +j)<=>* (* (a+i) +j) <=> (* (P+i)) [j]<=>(* (A+i)) [j]

Two, array of pointers
int *p[n]
Focus: []
High priority, first combined with p to an array, and then by int* that this is an integer pointer array that has an array element of n pointer types: that is, it is an array of n pointers.
This assignment is also wrong: P=a because P is a right value, P's value only exists p[0], p[1], p[2]...p[n-1], and they are pointer variables that can be used to hold variable addresses. But can be so *p=a; Here *p represents the value of the first element of the pointer array, the value of the first address of a.
To assign a two-dimensional array to an array of pointers:
int *p[3];
int a[3][4];
for (i = 0; i < 3; i++)
P[i] = A[i];
Here int *p[3] represents a one-dimensional array containing three pointer variables, respectively p[0], p[1], p[2], and by default these three pointer variables all point to NULL, so assign values separately.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.