Differences between array pointers and pointer Arrays

Source: Internet
Author: User

The difference between an array pointer and a pointer array is that the array pointer p is a pointer, And the pointer array p is an array that stores N pointer variables. I. array pointer int (* p) [n] key points: () High priority ([], () has the same priority, but their direction is from left to right, therefore, run * p in parentheses first. p is a pointer pointing to an integer one-dimensional array. The length of this one-dimensional array is n, or the step size of p. That is to say, when p + 1 is executed, p must span the length of n integer data (n * sizeof (int )). To assign a two-dimensional array to a pointer, the value should be: int a [3] [4]; int (* p) [4]; // This statement defines an array pointer pointing to a one-dimensional array containing four elements. P = a; // assign the first address of the Two-dimensional array to p, that is, a [0] Or & a [0] [0] p ++; // <=> a [1] <=> when p [1] is used to point to a two-dimensional array, its reference is the same as the array name reference, that is, a <=> p. For example, to represent an element a [I] [j] In column j of row I in the array: p [I] [j] <=> a [I] [j] <=> * (p [I] + j) <=> * (a [I] + j) <=> * (p + I) + j) <=> * (a + I) + j) <=> (* (p + I) [j] <=> (* (a + I) [j] 2. pointer array int * p [n] highlights: [] high priority. It is first combined with p to form an array, and then int * indicates that this is an integer pointer array, which has n Array elements of pointer type: that is, it is an array containing n pointers. This assignment is also incorrect: p = a; Because p is a right value, the value of p only exists in p [0], p [1], p [2]... p [n-1], and they are pointer variables that can be used to store variable addresses. But it can be like this * p = a; here * p represents the value of the first element of the pointer array and the value of the first address of. 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 three pointer variables exist in the memory of a one-dimensional array, which are p [0], p [1], and p [2]. By default, all three pointer variables point to NULL, so assign values respectively.

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.