The relationship between arrays and pointers in C language, and the array pointer in C Language

Source: Internet
Author: User

The relationship between arrays and pointers in C language, and the array pointer in C Language
In C language learning, pointer application is one of its essential parts. Many beginners are easily confused about this part. Today I am only going to talk about the relationship between pointers and array variables in C language, before talking about the relationship between them, I will first introduce two concepts: one is that pointers cannot be modified, and the other is that pointers cannot be modified. What do these two concepts mean? We will explain them through code:

1. int * const p1 = & I;

2. int const * p2 = & I;

We can find that when p1 and p2 are defined, the const and * are different, but their meanings are different. The code in 1 indicates that the pointer cannot be modified, and what is the pointer cannot be modified, that is, once the pointer is assigned to an address, it cannot

Point it to another address. For example, it is wrong to perform the P1 ++ operation on the pointer in 1, but the content in the pointer to the address can be changed, for example, you can use * p ++; or other integer values;

The code in 2 indicates that the pointer cannot be modified, that is, you cannot modify the content in the address pointed to by the pointer, that is, you cannot change the I through the P2 pointer, for example, this operation is incorrect: * P ++. Here you can

To modify the pointer, such as p ++;

Let's take a look at this writing method: 3. const int * p = & I; this writing method has the same meaning as 2.

Conclusion: const cannot modify the pointer before *. After *, the pointer itself cannot be modified.

Let's look at the array.

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

1. int arr [5] = {0, 1, 2, 3, 4 };

2. int buff [10] = {5, 6, 7, 8, 9 };

3. void test (int arr [], const int buff [])

{

...... // Omitted

}

4. int * p = arr [3];

5. a = p [-2];

We know that the array name itself is a pointer, and it points to the same content as the first element of the array, that is, arr = & arr [0]; we know that usually two pointers * p, * q: Yes. p = q can be used for such operations, but not between arrays.

Therefore, we cannot set: arr = buff; or & arr [5] = & buff [10]; this is because once the array is defined, the system will open up a fixed address for it, and we cannot make any changes to the address. We can see that this corresponds to our

The needle cannot be modified, so we cannot have such a statement as arr ++. Therefore, when we want to use a pointer to change the array value, it is usually as follows:

Int * p2 = arr []; // p2 points to arr [0]

P2 ++; // p2 points to arr [1]

~~~ This makes it clear that arrays are actually special pointers. In particular, their pointers cannot be modified and are fixed to an address.

In addition, we noticed that in the above Code 3, when we take the array name as a parameter, the pointer is actually passed. So what does the second parameter const int buff [] mean, it is consistent with the description at the beginning of this article.

Int * p = buff []; the function is equivalent to const int * p; that is, the pointer cannot be modified through the pointer. we pass in the pointer of the array buff, in the function, we cannot use this pointer to modify the buff array content. This method protects our data

.

Once we understand the array as a pointer, we can understand the 4 and 5 codes. We know that the array subscript cannot cross the border. For example, the above arr [-1] is incorrect, however, when we make * p = arr [3], we can have an expression like p [-2] Because the pointer can also

In the preceding example, what is p [-2]? In fact, because the pointer p initially points to the arr [3] address, p [-2] is equivalent to p-= 2; that is, it points to the address of arr [1.

So what if p [-5] Is it, will it report an error? I hope you can compile a small program for yourself and then I will know it. I will not talk about it here.

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.