The difference between an array pointer and an array of pointers

Source: Internet
Author: User
Tags arrays

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

Array pointers (also called row pointers)
define INT (*p) [n];
() high priority, the first explanation is that p is a pointer to an integer one-dimensional array, the length of the one-dimensional array is n, it can be said that the step of P. In other words, when executing p+1, p crosses the length of n integer data.

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. The execution of p+1 here is wrong, so the assignment is also 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];
for (i=0;i<3;i++)
P[i]=a[i];
Here int *p[3] indicates that a one-dimensional array holds three pointer variables, respectively p[0], p[1], p[2]
So you have to assign values separately.

The use of pointer arrays is primarily to address the following issues:



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: () >[]>*

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.