Complete the C language pointer third

Source: Internet
Author: User
Tags constant printf

1. Array elements

Look at the code below.

int i,a[]={3,4,5,6,7,3,7,4,4,6};
for (i=0;i<=9;i++)
{
printf ( “%d”, a[i] );
}

Obviously, it is the value of each element that shows an array of a.

We can also access elements like this, as follows

int i,a[]={3,4,5,6,7,3,7,4,4,6};
for (i=0;i<=9;i++)
{
printf ( “%d”,  *(a+i) );
}

The result is exactly the same as its effect.

2. Accessing array elements through pointers

int i,*pa,a[]={3,4,5,6,7,3,7,4,4,6};
pa =a  ;//请注意数组名a直接赋值给指针 pa
for (i=0;i<=9;i++)
{
printf ( “%d”, pa[i] );
}

Obviously, it also shows the values of each element of array A.

In addition to the array name can also be as follows:

int i,*pa,a[]={3,4,5,6,7,3,7,4,4,6};
pa =a;
for (i=0;i<=9;i++)
{
printf ( “%d”, *(pa+i) );
}

Look at the pa=a is the array name assignment to the pointer, and through the array name, pointer to the form of access to the elements, they do not make any difference, from here you can see that the array name is actually the pointer. Don't they have any difference? There, please continue.

3. The difference between an array name and a pointer variable

Take a look at the following code:

int i,*pa,a[]={3,4,5,6,7,3,7,4,4,6};
pa =a;
for (i=0;i<=9;i++)
{
printf ( “%d”, *pa );
pa++ ;  //注意这里,指针值被修改
}

As you can see, this code also outputs the values of each element of the array. However, you try to change the PA in {} to a. You will find that the program compiles with errors and cannot succeed. It seems that pointers and array names are still different. The above pointer is actually a pointer variable, and the array name is just a pointer constant. This code differs from the above code in that the pointer pa has a constant increment in the entire loop, that is, the pointer value is modified. The array name is a pointer constant whose value cannot be modified, so it cannot be done like this: a++. In the front 4, 5 section pa[i],* (pa+i), the value of the pointer pa is not changed at all. So the variable pointer pa and array name A can be interchanged.

4. Declare the pointer constant

Again, look at the following code:

int i, a[]={3,4,5,6,7,3,7,4,4,6};
int * const pa=a;//注意const的位置:不是 const int * pa,
for (i=0;i<=9;i++)
{
printf ( “%d”, *pa );
pa++ ;  //注意这里,指针值被修改
}

Does the code at this time compile successfully? No. Because the PA pointer is defined as a constant pointer. This is no different from the array name a. This also shows that the array name is the constant pointer. But...

int * Const A={3,4,5,6,7,3,7,4,4,6};//NOT

int a[]={3,4,5,6,7,3,7,4,4,6};//can be, so it must be so when initializing an array.

All of these are tested on the 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.