C language Facts are not simple: arrays and pointers

Source: Internet
Author: User

Before writing C, I didn't pay much attention to the array. Just define one. Then the prodded can be used. I just had a little problem later. After solving the decision to write such a blog, the array without pointers. Just put it all together.

Now I have defined an array: int cc[10];

There are several kinds of pointers around this array: cc, cc+1, &cc[0], &cc, &cc+1, and so on. Do you know what they all mean? Try executing the following code:

#include <stdio.h>int main () {int cc[10];    printf ("%x\n", CC);    printf ("%x\n", cc+1);    printf ("%x\n", &cc[0]);    printf ("%x\n", &CC);    printf ("%x\n", &cc+1);    GetChar (); return 0;}
CC, this is the first touch of the "pointer" to learn the array, the most familiar, it is the first element of the array.

Cc+1, which is a pointer to the second position of a number.

&cc[0], this is actually CC, which points to the first element of the array.

&CC. What is this thing? Pointer to pointer?

&cc+1, suppose the above means a pointer to a pointer. So this is not pointing to the wild address?

If the execution environment is a 32-bit machine. And the first address of the array is 0x28ff00. So:

The result of CC is 0X28FF00, there is no doubt about it.

The address of the cc+1 is 0x28ff04, not 0x28ff01, because an int occupies 4 bytes of space. Cc+1 is actually treated as cc+1*sizeof (int).

The result of &cc[0] is that 0x28ff00,cc[0] represents the first element of the array. Then &cc[0] nature is the address of the first element. &CC[0] = = CC.

&CC, this is hard to say. The value of the pointer cc is 0X28FF00,&CC the address of the pointer itself, how can we possibly know this address? is the output a random address? This output is completely meaningless if you have random numbers. What if it's not a random address, or 0x28ff00? This kind of words a is not equal to &a? It's obviously not right.

For basic types of pointers, such as int *tt; So *tt is its value, &TT is the address of the pointer, &tt! = TT

However, the above CC is a number of groups. As a matter of fact. &CC is compiled into &cc[0], but its meaning is different, &cc points to the beginning of the entire array.

The &CC and CC points can be used to represent the image:


As can be seen, &CC actually represents an int (*) [10], then &cc+1 can be understood as CC + sizeof (CC)/4, the reason is divided by 4 because the int pointer + + is actually moving 4 bytes.

Or,%CC = = cc + sizeof (CC)/4 = = cc + 10. So the value of &cc+1 is 0x28ff28.

It can be seen that our usual use of the array name, and not simply as a pointer to view. the essence of the array name is the variable name that represents the array object, which is an lvalue and an lvalue that cannot be changed.

However, because the size of the array is not saved in the program, it is only possible to access the array's left-hand values through the array name. You cannot access the right value of an array.

For this reason, the array name is given another new meaning when used as the right value-a pointer to the first element of the array. This is the array-to-pointer conversion rule.

According to the standard provisions. Only when the array name is the operand of the sizeof, & operator. It is an lvalue, and its type is an array type. In all other cases, the array name is an rvalue and is converted by the compiler itself to a pointer type, in which case we say that the array names are a pointer. And it's a pointer constant.



Next is something else interesting, and we combine sizeof with the array to output various values.

What is the output of the following program? Suggest thinking before executing the program to verify the answer.

#include <stdio.h>int main () {    int cc[10];    printf ("%d\n", sizeof (cc[0]));    printf ("%d\n", sizeof (CC));    printf ("%d\n", sizeof (&CC));    printf ("%d\n", sizeof (int (*) [ten]));    GetChar ();    return 0;}
sizeof (Cc[0]), the size of an int. Output 4, no problem.

sizeof (CC), be careful not to confuse it with the above. This is not a pointer to the first address of the array, and CC is the left value here. The array type. So the result is 40.

sizeof (&CC), how much should the answer be? Watch out. CC Here is also an lvalue, which is an array type, but &CC is different from CC, no matter how complex the array is always a pointer, the size of the pointer on the 32-bit machine is always 4 bytes. So the result is 4.

sizeof (int (*) [10]), which in fact is a &cc of the above, represents the entire array, but still a pointer, so the result is the same as 4.

Exercise: What the following program output is (if 32-bit machine):

#include <stdio.h>int main () {    int *p[2];    printf ("%d\n", *p);    printf ("%d\n", sizeof (p));    printf ("%d\n", sizeof (&P));    GetChar ();    return 0;}

The answer is: random number, 8, 4

One more exercise: what is the output of the following programs?

#include <stdio.h>int main () {    char str[]= "Hactrox";    char *p = str;    printf ("%d%d\n", sizeof (str), sizeof (p));    GetChar ();    return 0;}

Answer: 8, 4 (Do not forget to ' + ')


C language Facts are not simple: arrays and pointers

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.