C language two-bit arrays and pointers

Source: Internet
Author: User

Question, what is the result of the following output, and what is the difference?

voidMain () {inta[3][4] = {1,2,3,4,5,6,7,8,9,Ten, One, A }; //output of3 addresses are the same, where is the difference? printf"\n%x", a); printf ("\n%x",*a); printf ("\n%x",&a);
System ("Pause");}

Wild yards like me are basically ignorant of this problem, so let's first understand it through a one-dimensional array.

voidMain () {intnum[5] = {1,2,3,4,5 }; printf ("\n%x", num);//initial address of first element, 1 int elements 4 bytesprintf"\n%x", &num);//an array of first addresses, an array of 5 elements, and 20 bytes. //printf ("\n%d", (* (&num)) [2]); you Know (* (&num)) is an array {1,2,3,4,5}-> &num is an address to the entire array//differenceprintf"\n%d",sizeof(*num));//4 *num Extract content by type address = 1, 4 bytesprintf"\n%d",sizeof(*&num));//*&num extract content by type addressGetChar ();}

By the above example, you can know

1. Num is the first address to the array, so the *num is the value of the first address of the array 1.

2. &num is the address that points to the entire array.

So let's look at an example of a two-dimensional array above.

1 voidMain ()2 {3     inta[3][4] = {1,2,3,4,5,6,7,8,9,Ten, One, A };4     5     //print out the entire array of elements and corresponding addresses6      for(inti =0; I <3; i++)7     {8          for(intj =0; J <4; J + +)9         {Tenprintf"%d,%x", * (* (a+i) +j), * (A + i) +j); One         } Aprintf"\ n"); -     } -  the     //3 results are the same, where is the difference?  -printf"\n%x", a);//A-line pointer -printf"\n%x", *a);//the value of the A-line pointer {1,2,3,4} -printf"\n%x", &a);//address of the entire two-dimensional array +      -printf"\n%d",sizeof(*a));//*a represents an array of a row {1,2,3,4}, +printf"\n%d",sizeof(**a));//4, there can be inferred that **a represents the first row array first address 1 Aprintf"\n%d",sizeof(*&a));//&a represents the first address of the entire two-dimensional array, then * (&a) takes the contents of the entire two-dimensional array, 12 elements, 48 bytes at  -  -System"Pause"); -}

  

C language two-bit arrays 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.