Complete C language pointer Article 3

Source: Internet
Author: User

1. array elements

See the following:Code

 
IntI, a [] = {3,4,5,6,7,3,7,4,4,6};
For(I =0; I <=9; I ++)
{
Printf ("% d", a [I]);
}

Obviously, it shows the element values of array.

We can also access the element as follows:

IntI, a [] = {3,4,5,6,7,3,7,4,4,6};
For(I =0; I <=9; I ++)
{
Printf ("% d", * (a + I ));
}

The results and functions are exactly the same.

2. Access array elements through pointers

IntI, * pA, a [] = {3,4,5,6,7,3,7,4,4,6};
Pa =;//Please note that array name a is directly assigned to pointer Pa
For(I =0; I <=9; I ++)
{
Printf ("% d", pa [I]);
}

Obviously, it also displays the element values of array.

In addition, it can be the same as the array name:

IntI, * pA, a [] = {3,4,5,6,7,3,7,4,4,6};
Pa =;
For(I =0; I <=9; I ++)
{
Printf ("% d", * (PA + I ));
}

Look at Pa = A, that is, the array name is assigned to the pointer, and there is no difference between them through the array name, pointer access to elements, from this we can see that the array name is actually a pointer. Are there any differences between them? Yes. Continue.

3. Differences between array names and pointer Variables

See the following code:

  int  I, * pA, a [] = { 3 ,  4 ,  5 ,  6 ,  7 ,  3 ,  7 ,  4 ,  4 ,  6 }; 
Pa =;
for (I = 0 ; I <= 9 ; I ++)
{< br> printf ("% d", * pA);
PA ++; /// note that the pointer value is modified
}

As you can see, this code also outputs the values of each element in the array. However, try changing the PA in {} to. You will findProgramCompilation failed. It seems that the pointer and array name are still different. In fact, the aboveThe pointer is a pointer variable, while the array name is only a pointer constant.. The difference between this code and the above Code is that the value of pointer PA is continuously increasing throughout the cycle, that is, the pointer value is modified. The array name is a pointer constant and its value cannot be modified. Therefore, it cannot be operated like this: A ++. at pa [I] And * (PA + I) in section 4 and 5 above, the value of pointer PA is to make the final change. So the variable pointer Pa and array name A can be exchanged.

4. Declare pointer Constants

See the following code:

  int  I, a [] = { 3 ,  4 ,  5 ,  6 ,  7 ,  3 ,  7 ,  4 ,  4 ,  6 }; 
int * const Pa = A; /// note the position of const: Not const int * pA,
for (I = 0 ; I <= 9 ; I ++)
{< br> printf (" % d ", * pA);
PA ++; // note the following, pointer value modified
}

Can the code be compiled at this time? No. Because the PA pointer is defined as a constant pointer. In this case, it is no different from array name. This shows that the array name is a constant pointer. But...

Int * const A = {3, 4, 5, 6, 7, 3, 7, 4, 4}; // No

Int A [] = {3, 4, 5, 6, 7, 3, 7, 4, 4}; // yes, so this is the case when initializing an array.

The above are all labs on vc6.0.

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.