Relationship of array variables and pointers

Source: Internet
Author: User

The teacher said that the array variable can be regarded as a const pointer variable, in the end is "can be considered", or "is" pointer?

Use procedural evidence to illustrate your point of view.

(Hint: If all operations on a const pointer can be made to an array variable, and the result is consistent, it means that the variable is a pointer; If an operation cannot be done, or the result is inconsistent, it is not a pointer)

    #include <stdio.h>    intMain () {intA[] = {1,1,2,3,4,5,6,7,8,9,0,};int *constp = &a[0];printf("sizeof (a) = %d , sizeof (p) = %d \ n"), sizeof (a), sizeof (p));//sizeof(a) shown as -, which is the entire length of the array, sizeof (p) is displayed as8; Is the length of the pointerprintf("main:p = %p\ n", p);printf("main:a = %p\ n", a);//In the main function is still the first address of the array a[ -] =Ten;printf("main:a[13] = %d\ n", a[ -]); p[ -] =Ten;printf("main:p[13] = %d\ n", p[ -]);//Sum can be executed successfully in the main function (A, One); }intSumintA[],intLen) {intsum =0;inti =0;int *constp = &a[0];//    int *constA = &a[0];//[Error]' A 'Redeclared as different kind of symbol (a re-declared as different types of symbols)//int *constp = &a[0];//[Error] redefinition of' P '(P repeat definition)//The above two hints are different errors, stating that the array parameter is not actually a constant pointer of this type//int* A = &a[0]; -[Error]' A 'Redeclared as different kind of symbol//int *constp = &a[0];//[Error] redefinition of' P 'Description array parameter is not a pointerprintf("p = %p\ n", p);printf("a = %p\ n", a);//He was pointing to an array of p[1] =Ten;printf("p[1] = %d\ n"-P:1]); a[1] =Ten;printf("a[1] = %d\ n", a[1]);//Whichever of the above executes is the same, and can be executed, which means that the variable is modified, the access variable is the same a[ -] =Ten;printf("a[13] = %d\ n", a[ -]); p[ -] =Ten;printf("p[13] = %d\ n", p[ -]);//This shows that a also does not store information about his length, it is just an address, like P, two without warning, can compile the same result for(i =0; i < len;i++) {sum + = A[i]; }printf("sizeof (a) = %d \ n", sizeof (a));//By compiling look to see sizeof (a) is still8, from this point of view, the passed array parameter is exactly the same as the constant pointerreturnSum }//From the above test, when the array variable as a parameter, it is not a strict pointer, not a constant pointer, but he can be seen as a pointer, and the operation of the constant pointer can be applied to the array variable//when the array variable is not a parameter, that is, it is in the main function, it has two meanings, When used on sizeof, it seems to be considered an array as a whole, but in other cases it can be treated as a constant pointer.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Relationship of array variables 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.