The difference between array name and pointer variable in C language

Source: Internet
Author: User

How the compiler works: on a 64-bit computer, when you create a pointer variable, the computer allocates 8 bytes of storage space for it. But what if you are creating an array? The computer allocates storage for an array, but does not allocate any space to the array variable, and the compiler replaces it with the starting address of the arrays only where it appears.

Conclusion 1: Because the computer does not allocate space for an array variable, it cannot be pointed to elsewhere. Examples:

Char s[]="How arebig is it? " " ; Char *t=s;   // correct, assign the address of the array to the pointer variable ts=t;        // error, array variable has no storage space, cannot store value of pointer variable t, compile error

Conclusion 2: If an array variable is & with the address character, the result is the variable itself.

#include <stdio.h>intMainintargcChar*argv[]) {    intarr[3]={1,2,3}; printf ("arr=%p\n", arr); printf ("&arr=%p\n",&arr); return 0;} The result is: [email protected]:~$ ./Tarr=0x7ffed97ce8d0&arr=0x7ffed97ce8d0

Conclusion the value of 3:sizeof (array) is the size of the array, and sizeof (pointer) is the size of the previous address of the operating system, 8 bytes on the 64-bit machine, and 4 bytes on the 32-bit machine.

#include <stdio.h>intMainintargcChar*argv[]) {    intarr[3]={1,2,3}; int*p=arr; printf ("sizeof (arr) =%d\n",(int)sizeof(arr)); printf ("sizeof (p) =%d\n",(int)sizeof(p)); return 0;} Output: [email protected]:~$ ./Tsizeof(arr) = Asizeof(p) =8

Conclusion 3 shows that if the array variable is assigned to a pointer, then the pointer variable will only contain the address information of the array, and the length of the arrays is unknown, which is equivalent to the loss of some information by the pointer. We call this loss of information a degenerate. As long as the array variable is passed to the function, the array is inevitably degraded to a pointer, so passing the array to the function requires a clear indication of the size of the array. To this:

int arr[3]={1,2,3}; int sum_arr (int arr[],int n);   // explicitly indicate array size with integer variable n

The difference between array name and pointer variable in C language

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.