Arrays and pointers (i)

Source: Internet
Author: User

for arrays, the most headache is the array-to-pointer relationship. In my opinion, the key to understanding the relationship between arrays and pointers is to understand the correlation types of pointers (each pointer has a data type associated with it). Because the length of the article is relatively long, so divided into two, this is the first article ~

One-dimensional arrays and first-level pointers

First, we start with a one-dimensional array and a first-level pointer to this esoteric process of understanding.

int a[3] = {123};  

The above code is a familiar way of creating a one-dimensional array, and what we need to know about this code is that the array occupies a contiguous amount of storage space in memory, asshown in 01. Second, the variable name of the array is the address of the first element in the array, that is, the array name is both a variable name and a pointer.

pointers and pointer variables are strictly different. The address of an object is used to indicate where the object is stored, called a pointer, and the ability to store an address worthy variable is called a variable of pointer type, referred to as pointer variable!  

our focus is on the meaning of the array variable name. As mentioned above, the variable name of the array is the address of the first data in the array, that is, in the above code,A is the address of a[0], which is essential for understanding the following (02).

Let's take a look at this piece of code:

1 int a[3] = {123};   2 int *p; 3 p = A;  

Question 1: Why can a one-dimensional array variable name A be assigned to a first-level pointer p?

Because the array variable name A is the address of the first data in the array, and the array stores the data type int, that is, a is an address of type int (that is, the association type of a is int). The first-class pointer variable p is defined as storing an address of type int, so p=a is perfectly fine (i.e. assigning an int type address to a pointer variable that stores an int type address). Here, we use the key mentioned above, the variable name of the array is the address of the first data in the array, and the core I mentioned, the type of the pointer Association.

Question 2:02 shows, why A+1 is the second data of the array?

To understand this problem more accurately, we need to understand the correlation types of pointers and pointer operations. First, as we have already said, the array variable name A is the address of the first data in the array, and it is clear thatthe association type of a is int. In C + +, pointers are added and reduced by the number of bytes of the data type associated with the pointer, anda+1 means that a moves the number of bytes of an int type. So the meaning of a+1 is to move to the next int data.  

For this question, we need to extend, do you think this code is correct?

 int  a[3 ] = {1 , 2 , };  2  int  *  P;  3  p = &a;  

        question answer ... It's wrong to say it first. For the fetch operator &, one of the key points to understand is that the accessor operator & a is an integer array of length 3, i.e. int[3], so &a returns a pointer with an association type of int[3]. The p stores a pointer variable with an association type of int, so the code above is wrong and the correct code is as follows:

1 int a[3] = {123};   2 int (*p) [3];   3 p = &a;  

Together, we come to the following core points:

    • The variable name of an array represents both a variable and a pointer

    • Each pointer has an associated variable

    • The FETCH operator & Returns a pointer to the data type of the variable associated with the type

    • Pointers can only be assigned to pointer variables that store the same type of association

Respect other people's knowledge achievements, reprint /quote Please specify the source ~

by Miao

2015/3/12

Arrays and pointers (i)

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.