C pointer (3) pointer and array

Source: Internet
Author: User

C pointer (3) pointer and array

(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 <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Capacity + O3 + 83 qyKu/capacity + CjxwPiAgICC/capacity + capacity/cjrW13Uy8vjt/uhozwvcD4KPHA + ICAgIL7Nz/capacity ++ capacity + authorization + vK/authorization + 0JiMyMDI4NDvK/authorization + myos7eudjPtaGjPC9wPgo8cD48YnI + c?vcd4kpha + authorization + 0 s/Y8L3A + authorization = "brush: java; "> void fun (int * array) {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


From the running result, the pointer and array are indeed different. 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;

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. 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 [], int), array is still regarded as a pointer. That is to say, even if the array contains a length such as int array [5], the length will be ignored by the compiler. In a word, all arrays in the form parameters are treated as pointers.
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.
Another point is that 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.

So why does int (*) [2] represent an array pointer? This requires a thorough understanding of c's statement syntax, which will be explained in the subsequent order.





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.