C pointer (3) pointer and array

Source: Internet
Author: User
Tags scalar

(3) pointers and Arrays

In C, pointers and arrays seem to be inextricably linked. In fact, they are not the same thing: the pointer is a pointer, the array is an array, and the two are different.

They are related, but it is because such code is common:

int main(){int array[] = {1,2,3,4,5};int n = sizeof(array) / sizeof(int);int *p = array;int i;for (i = 0; i < n; i++)printf("p[%d]...%d\n", i, p[i]);system("pause");return 0;}
Run


In the above Code, the combination of pointer and subscript operator gives a pointer and array the same feeling.

In essence, the array name is a constant pointer to the starting element of the array. This is also the only link between arrays and pointers!

The reason why P [I] can be used to access elements in the array is that P [I] is interpreted as * (p + I) in the compiler, which is still a pointer function. For the compiler, it is meaningless to use P [I] to express the meaning of * (p + I), just to make people look comfortable and convenient. This isSyntactic sugar:

P [I] is a simple method of writing * (p + I). In fact, for the compiler at least, [] can be completely absent.

However, for humans, the writing of * (p + I) is difficult to interpret and difficult to write (large amounts of input ). Therefore, the [] operator is introduced in C language.

Just like this, these functions are just introduced to make it easy for humans to understand. They can indeed make us feel the sweetness of programming languages (easy to start ), sometimes we call these functions syntax sugar or syntactic sugar ).

From conquer the C pointer, we recommend this book. [] Indicates the meaning of an array. In an expression, [] is irrelevant to an array!

In summary, the usage of seemingly Arrays: P [I] is actually the syntactic sugar of * (p + I). P is still a pointer and has nothing to do with arrays.


The differences between pointers and arrays can also be seen from the example below

void fun(int array[5]){printf("  sizeof(array)...%d\n", sizeof(array));}int main(){int array[] = { 1, 2, 3, 4, 5 };printf("  sizeof(array)...%d\n", sizeof(array));fun(array);return 0;}
Run


According to the running results, although the function parameters are declared using arrays, they are still treated as pointers. This reveals the rule for passing an array in C: The address passed in the past is the address pointing to the starting element of the array. This is based on two points;

  1. In terms of efficiency, if the entire array is assigned a value, it is too time-consuming and consumes space. It is better to upload the address and use the same content.
  2. At the beginning of C language design, the assignment operation was limited to basic types (char, Int, float ......), Arrays are aggregate types.
This gives us the programming inspiration: When passing an array, do not forget to pass the array size. Otherwise, the function is easy to cross-border because it does not know the array size. The void fun (int * array, int N) function should be designed in this way, and N is the array size. It also needs to be pointed out that even if the function is designed as void fun (INT array [5], int N), array is still regarded as a pointer. That is to say, even if the array contains a length, the length will be ignored by the compiler. In a word, all arrays in the form parameters are treated as pointers. In this case, it is better to directly write void fun (int * array, int N ). Pointer form, more expressive intention.

Another thought: If P [I] means * (p + I), because addition has an exchange law: P + I = I + P, so I [p] can also express the meaning of P [I]. Is that true? Lab Verification:
int main(){int array[] = { 1, 2, 3, 4, 5 };int n = sizeof(array) / sizeof(int);int *p = array;int i;for (i = 0; i < n; i++)printf("  %d[p]...%d\n", i, i[p]);return 0;}
Run
Experiments show that our conjecture is true: P [I] is indeed the syntactic sugar of * (p + I. I [p] Is it against the day!
Conclusion: only in function parameters, the declared array, such as int array [], is regarded as a pointer. In other cases, the pointer is not associated with the array.
The meaning of & array is another point. For int array [5]; array, which indicates a pointer to the starting element of the array, what is & array? Experiment:
int main(){int array[] = { 1, 2};printf("   array...%p\n", array);printf("  &array...%p\n", &array);printf("&array+1...%p\n", &array+1);return 0;}
Run
Analysis results: the difference between 0031fcec and 0031fce4 is 8, while that of sizeof (array) is 8. The conclusion is that both array and & array are pointers, but their types are different. The array type is int *, and the & array type is int (*) [2]. Array is a pointer to a common int type. & array is an array pointer. The array element is of the int type and the array size is 2. The values of array and & array are the same.
Scalar: in short, scalar refers to numeric types such as char, Int, double, and enumeration types, plus pointers. As for the types of arrays, struct, and shared objects that combine multiple scalar values, we call them aggregate types ).

So why does int (*) [2] represent an array pointer? This requires a thorough understanding of C's declaration syntax. For example, what type of pointer is the array name of a two-dimensional array (or, more importantly, a multi-dimensional array? In this case, you need to understand the actual meaning of the array in C and explain it in the subsequent order.





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.