The C language array name and the address of the logarithm group name

Source: Internet
Author: User

http://blog.csdn.net/zdcsky123/article/details/6517811

I believe a lot of C beginners know that the array name is equivalent to a pointer to the first address of the array, and the function name is equivalent to the function pointer, pointing to the function's entry address. Now again, if the name of the group to take the address, then what will get it? A lot of people immediately think: to the pointer to address, is the pointer of pointers, both two-level pointers! Of course, this conclusion is wrong, otherwise this note will be meaningless.


Let's step through the analysis, here is a code to verify the problem

Code:
  1. #include <stdio.h>
  2. int Main ()
  3. {
  4. int a[10];
  5. printf ("a:/t%p/n", a);
  6. printf ("&a:/t%p/n", &a);
  7. printf ("a+1:/t%p/n", a+1);
  8. printf ("&a+1:/t%p/n", &a+1);
  9. return 0;
  10. }

You can compile and run it, and the result of my output is:

Code:
    1. /*
    2. A:0012ff20
    3. &a:0012ff20
    4. A+1:0012ff24
    5. &a+1:0012ff48
    6. */

A and &a point to the same piece of address, but they have a different effect after +1, a+1 is the memory size of an element (increase 4), and &a+1 increases the memory size of the entire array (40 increase). Both A and &a point and &a[0] are the same, but the properties are different!

Read here, a lot of friends have already understood the mechanism, if still somewhat vague, please continue to look down

Code:
    1. int Main ()
    2. {
    3. int a[10];
    4. printf ("%d/n", sizeof (a));
    5. return 0;
    6. }

This code will output the memory size of the entire array, not the size of the first element, so whether we are contacted, sizeof (a) where A and
&a some similarities?! Yes, yes, &a is the address of the entire array! Both the array name fetch address is equivalent to the logarithmic group fetch address.


Well, let's summarize, if you still don't understand, don't worry, the following concepts are clear


In fact a and &a results are the first address of the array, but their type is not the same.
A is &a[0], that is, the first element of the array takes the address, a+1 represents the first address +sizeof (element type).
&a Although the value is an array of the first element address, but the type is: type (*) [number of array elements], so the &a+1 size is: The first address +sizeof (a).

There is the wrong conclusion of the pointer of the first reference of this article, in fact, even if you do not understand the above, it should be judged that the conclusion is wrong, you should know that the array name is the first address of the array at the same time, it should be known that the array name is only "quite" to the pointer, not really a pointer, The array name is just a constant (a constant value for the address of the first element of the group), so the + + or--operation cannot be performed. But the constant is unable to take address, and the reason why there is &a, in fact, the meaning of a here is not the original number of the group name, it represents the entire array.

The C language array name and the address of the logarithm group name

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.