The difference between an array pointer and an array of pointers

Source: Internet
Author: User

To assign a two-dimensional array to a pointer, you should assign this value:
int a[3][4];
int (*p) [4]; The statement defines an array pointer to a one-dimensional array with 4 elements.
P=a; Assign the first address of the two-dimensional array to p, i.e. a[0] or &a[0][0]
p++; After the statement executes, that is, P=p+1;p crosses line a[0][] points to the line a[1][]

So an array pointer is also called a pointer to a one-dimensional array, also known as a row pointer.

Array of pointers
define int *p[n];
[] High priority, the first combination with P as an array, and then by int* that this is an integer pointer array, it has n pointer type array elements. When P+1 is executed, p points to the next array element, so that the assignment is wrong: P=a, because P is an unknown representation, only 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 a pointer array:
int *p[3];
int a[3][4]; p++; The statement indicates that the P array points to the next array element. Note: Each element of this array is a pointer
for (i=0;i<3;i++)
P[i]=a[i] here int *p[3] means that a one-dimensional array holds three pointer variables, respectively p[0], p[1], p[2]
So you have to assign values separately.

So the difference between the two is enlightened, the array pointer is just a pointer variable, it seems that the C language is specifically used to point to a two-dimensional array, it occupies memory of a pointer to the storage space. Pointer arrays are multiple pointer variables that exist in the form of an array of memory, occupying multiple pointers of storage space.
It is also important to note that both the reference and the array name reference are the same when used to point to a two-dimensional array.
For example, to represent an array of I row J columns of an element:
* (P[I]+J), * (* (p+i) +j), (* (P+i)) [j], P[i][j]

Priority: () >[]>*

Source: http://www.cnblogs.com/hongcha717/archive/2010/10/24/1859780.html

========================================================================= the memory layout of pointer arrays and array pointers beginners always distinguish between pointer arrays and array pointers. In fact, very well understood:
Array of pointers: first it is an array, the elements of the array are pointers, the number of bytes of the array is determined by the size of the array itself, each element is a pointer, and any type of pointer under a 32-bit system is always 4 bytes. It is the abbreviation for "array of stored pointers".
Array pointer: First it is a pointer to an array. Any type of pointer under a 32-bit system is always 4 bytes, as to how many bytes it points to, not knowing, the size of the array. It is the abbreviation for "pointers to arrays".

Which of the following is an array pointer, which is an array of pointers:
A
int *p1[10];
B
int (*P2) [10];
Every time I ask this question in class, there is always something unclear. Here you need to understand the priority problem between a symbol.

The priority of "[]" is higher than "*". P1 is first combined with "[]" to form the definition of an array, with the array named P1,int * decorated with the contents of the array, that is, each element of the array. Now we know that this is an array that contains 10 pointers to data of type int, that is, an array of pointers. As for P2, it is better understood that the precedence of "()" Here is higher than "[]", "*" and P2 constitute a pointer definition, and the pointer variable named P2,int modifies the contents of the array, that is, each element of the array. The array does not have a name here and is an anonymous array. So now we know that P2 is a pointer to an array that contains 10 data of type int, that is, an array pointer. We can use the following diagram to deepen our understanding:

The difference between an array pointer and an array of pointers

Related Article

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.