C language is not simple: arrays and pointers

Source: Internet
Author: User

C language is not simple: arrays and pointers

When I was writing C, I didn't pay much attention to the array. So I defined one and used it upside down. But then I encountered a problem. After the solution, I decided to write such a blog. The array cannot be separated from the pointer, So I simply put it together.

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

There are several pointers around this array: cc, cc + 1, & cc [0],& Cc, & cc + 1And so on. Do you know what they mean? Run the following code:

#include 
 
  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, which is the first "Pointer" to be exposed when learning arrays. It is the first element of arrays.

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

& Cc [0], which is actually cc, pointing to the first element of the array.

& Cc, what is this? Pointer to pointer?

& Cc + 1. If the above is a pointer to a pointer, isn't it a pointer to a wild address?

Assume that the running environment is a 32-bit server and the first address of the array is 0x28ff00:

There is no doubt that the cc result is 0x28ff00.

The cc + 1 address is 0x28ff04 rather than 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 0x28ff00, and cc [0] indicates the first element of the array. Then & cc [0] is the address of the first element. & Cc [0] = cc.

& Cc, this is hard to say. The value of the pointer cc is 0x28ff00, and & cc indicates the address of the pointer. How can we know this address? Is the output random address? The random number is meaningless. Isn't it a random address or 0x28ff00? In this case, a is equal to &? Obviously not...

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

However, the above cc is an array. In fact, & cc is compiled into & cc [0], but it has different meanings. & cc points to the beginning of the entire array. & Cc and cc points can be used for image representation:


As you can see, & cc actually represents int (*) [10], so & cc + 1 can be understood as cc + sizeof (cc)/4, the reason for dividing the value by 4 is that the int pointer ++ actually moves four bytes.

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

We can see that the array name we usually use is not simply treated as a pointer. An array name represents the variable name of an array object. It is a left value and a left value that cannot be changed. However, because the size of the array is not saved in the program, only the left value of the array can be accessed through the array name, and the right value of the array cannot be accessed. For this reason, when the array name is used as the right value, it is given another new meaning-the pointer to the first element of the array, which is the array-to-pointer conversion rule. According to the standard, only when the array name is used as the operand of sizeof and & operator, it is a left value and its type is array. In all other cases, the array name is a right value and is automatically converted to the pointer type by the compiler. In this case, the array name is a pointer and a pointer constant.


Next is something interesting. We can combine sizeof with the array to output various values. What are the output results of the following programs? We recommend that you think about it and then run the program to verify the answer.

#include 
 
  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(*)[10]));    getchar();    return 0;}
 
Sizeof (cc [0]), the size of an int, output 4, no problem.

Sizeof (cc). Do not mix it with the above. This is not the pointer to the first address of the array. cc here is the left value, which is an array type, so the result is 40.

Sizeof (& cc), what is the answer? Note that cc is still the left value here. It is an array type, but & cc is different from cc. No matter how complicated the array is, it is always a pointer, the pointer size on a 32-bit machine is always 4 bytes, so the result is 4.

Sizeof (int (*) [10]), which is similar to the above & cc, representing the entire array, but still a pointer, so the result is also 4.

Exercise: What is the output result of the following Program (assuming a 32-bit server ):

#include 
 
  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

Another exercise: What is the output of the following programs?

#include 
 
  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 '\ 0 ')


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.