"C Expert Programming" chapter fourth-Shocking facts: arrays and pointers are not the same

Source: Internet
Author: User

Arrays and pointers are two of the most important parts of the C language, and they are the easiest two places for novice programmers to get confused, and in this chapter we lock pointers and arrays to explore their similarities and differences.

First look at the difference between pointers and arrays on declarations:

 int a[10];

int *p;

Obviously, the first one is the array A, the second is the pointer p. The next question is what is the type of a? What is the type of P? A[0] is of type int, and A is a group name, does it represent the entire array? This is not the case, a is a pointer constant, a pointer constant pointing to int, and p is a pointer to int, which is a variable. This is their first difference : One is a constant and the other is a variable. Then there is a difference between a constant and a variable, and they all exist. For example, variables can be assigned to each other, while constants are not, as in the following example:

  int a[10];

int b[10];

int *c;

Both A and B are array names, constant pointers, and C is a variable, so you can assign a value to C, for example C = &a[0], or C = A, no problem, but think about B = A, and you'll find this statement illegal if you want to copy an array, Only one variable can be assigned with a loop.

The second important difference is that they are accessed in different ways , although their access forms may look like subscript references.

  Char a[] = "HelloWorld";

Char *p = "HelloWorld";

For example, in the above two definitions, if you look at the value of a[3], then it must be ' l ', but the same you can see p[3], the answer is also ' l ', but the way of access slightly different:

First See a[3], which is equivalent to * (A + 3), we assume that a constant pointer to the address of 1000, then the array in memory may be such a child:

  

directly into the address of a to see, then is * (1000 + 3), is the number 1003th address, the obvious is the character ' l '. Let's look at the way pointers are accessed:

This "Helloworld" is a string constant, the system will allocate a piece of memory, 2345 this address is randomly assigned, the pointer address 1032 is also random, so that the pointer p to a constant string is to change the contents of address 1032 to the first address of the string constant, So now to see the meaning of p[3], the first to take P's address of the content is 2345, and then the subscript is represented by the offset of the value of the pointer, resulting in an address, then is 2345+3, this address is 2348, and then on the 2348 this address to access, thereby getting the character ' l '.

It may be simpler to use char as a type, where you can omit a detail, and if you use int, the details will be noticed, that is, the way they compute the address, for example, with an int as the type.

  int a[10];

int *p;

Also consider a[3] and p[3], this problem comes, char in memory just a byte, and int, generally speaking, 32-bit machine accounted for 4 bytes, then a[3] is also equal to * (A + 3)? That is obviously wrong, because assuming a[0] address is 1000, then a[1] address is 1004, A + 3 is the value of 1003, how much should be taken? The answer is that this should be calculated here, which equals:

  * (A + 3 * int)

To multiply a type length, so it is actually * (A + 3 * 4), the address is 1012,.

What about P[3]? Also, because the pointer is of different type, the pointer is multiplied by the length value of an int.

After a discussion of two types of access, we can see that if you confuse the declaration with the definition, it's going to be a problem, for example, you define an array char p[10], but you make such a declaration when you use it in other files extern char *p, then you're in big trouble, Because the array and pointers are accessed differently, the value in your array may be used as an address, a value as an address, and a value inside it, which is a garbage value.

Of course, in addition to the way they are accessed, there are different ways to save data :

Still, as in the example above, when you declare an array, it actually assigns you an area, and when you declare a pointer, it allocates only the memory of the pointer variable, the value pointed to by the pointer does not allocate memory, and it is only useful for characters, such as you can: char *p = " HelloWorld ", however, you cannot do this: float *p = 3.14, this statement is illegal.

The main difference is in the above points, and the following is the similarities between them: the most obvious thing is that when the array name is passed as a function parameter:

  int function (char a[]);

Like the above statement is definitely not a problem, it shows that a character array is passed as a parameter to the function functions, but it does not actually explain the size of the array? Think about it, you will find this usage quite witty, there are a few reasons: first of all, this means that all arrays, regardless of size, can be used as parameters of the same function, you think, if the processing only two elements of the array and only three elements of the array to write different functions, it would be a pretty scary thing;

The following merit is based on its transitive behavior, the above statement is equivalent to this:

  int function (char *a);

This can be well applied to a property of a function: All parameter passing is a value call, that is, the formal parameter is only a copy of the argument, in function, because the parameters are passed to the original array a copy of the pointer, so you can rest assured that the use of a++ and other statements, Instead of worrying about the compiler warning you to modify the array name.

As can be seen from the above comparison, pointers and arrays are closely related, both similar and there is a great difference, so we must be careful when using the above only to list the main differences between them and the main similarities, as for some other relations, we can refer to other information.

"C Expert Programming" chapter fourth-Shocking facts: arrays and pointers are not the same

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.