C Primer Plus 5th Reading Notes-pointers and multi-dimensional arrays

Source: Internet
Author: User

Based on regression, I feel that there is a clear explanation of the relationship between pointers and multi-dimensional arrays in C Primer Plus 5th. Therefore, this blog post is a supplement to the notes that have not been written down before.

------------------------------------------------------------ Split line ---------------------------------------------------------

For a simple discussion, we use a small array. Assume that the following statements are made:

Int zippo [4] [2];/* array of integer arrays */

The array name zippo is also the address of the first element of the array (one of the theorem ). In this example, the first element of zippo is an array containing two int elements. Therefore, zippo is also an array containing two int elements.
. The following is a further analysis of pointer attributes:

@ Because zippo is the address of the first element of the array, the zippo value is the same as & zippo [0. On the other hand, zippo [0] itself is an array containing two integers, so zippo [0] value (an array containing two integers, I think it can be considered as an array name) it is the same as the address of the first element (an integer) & zippo [0] [0. In short, zippo [0] is the address of an integer-sized object, while zippo is the address of Two integer-sized objects. Because an array composed of an integer and two integers starts from the same address, zippo and zippo [0] have the same value.


@ Adding 1 to A pointer (that is, an address) adds a value of the corresponding type to the original value. In this respect, zippo and zippo [0] are different. The size of the zippo pointing to an object is two int (an array consisting of two integers ), the size of the object pointed to by zippo [0] is an int (an array containing two integers ). Therefore, zippo + 1 and zippo [0] + 1 have different results.


@ The value of a pointer (that is, an address) (using the operator * or the [] Operator with an index) is the value of the object to which the Pointer Points. Because zippo [0] (an array containing two integers) is the address of its first element zippo [0] [0], * (zippo [0]) indicates the value stored in zippo [0] [0], that is, an int value. Similarly, * zippo indicates the value of zippo [0], But zippo [0] itself is an int address, that is, & zippo [0] [0], therefore, * zippo is & zippo [0] [0]. Apply the value operator to these two expressions at the same time to obtain the ** zippo equivalent to * & zippo [0] [0], the latter is simplified to an int number zippo [0] [0]. In short, zippo is the address of an address. It takes two values to obtain the common value. The address or pointer of an address is a typical example of double indirect.

----------------------- Split line -----------------

These two paragraphs seem complicated. In fact, you can understand them by figuring out the two points. Zippo is an array with array elements. zippo [0], zippo [1], zippo [2], and so on are all arrays. Since these are arrays, zippo [X] represents the address of the array.


Attached book code:

#include<stdio.h>int main(void){int zippo[4][2] = {{2,4},{6,8},{1,3},{5,7}} ;printf("zippo = %p, zippo + 1 = %p\n",zippo,zippo+1) ;printf("zippo[0] = %p, zippo[0] + 1 = %p\n",zippo[0],zippo[0]+1) ;printf("*zippo = %p, *zippo + 1 = %p\n",*zippo,*zippo+1) ;printf("zippo[0][0] = %d\n",zippo[0][0]) ;printf("*zippo[0] = %d\n",*zippo[0]) ;printf("**zippo = %d\n",**zippo) ;printf("zippo[2][1] = %d\n",zippo[2][1]) ;printf("*(*(zippo+2)+1) = %d\n",*(*(zippo+2)+1)) ;return 0 ;}




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.