C language array pointer, array pointer

Source: Internet
Author: User

C language array pointer, array pointer

I learned the array pointer over the past two days and it is very painful.

The first is a simple one-dimensional array.

Defines an array int arr [5]. arr is the int type pointer pointing to the first element of the array. arr + 1 is the int type pointer of the second element of the array, * arr is the value corresponding to this pointer, which is easy to understand.

Int arr [5] = {1, 2, 3, 4, 5}; // defines the array printf ("\ n % x", * arr ); // 1 = arr [0] printf ("\ n % x", * (arr + 1); // 2 = arr [1]


It hurts to get to the two-dimensional array. After searching for a long time, I found a sentence to explain my doubts, and then everything was solved.

Define a two-dimensional array int arr [2] [3]

int arr[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };printf("\n%d", sizeof(*(&arr)));//24printf("\n%d", sizeof(*arr));//12

Two-dimensional arrays are divided into rows and columns. This is an array of two rows and three columns,

The first one is & arr is the pointer to the entire array. The corresponding type is int [2] [3], so the size of its corresponding value is 24 (each number occupies four bytes ).

The second arr represents the array pointer of the first line. You may need to move your mind here. In the first example, the one-dimensional array int arr [3, by default, arr is the pointer corresponding to the first element. The type is int, and the increment of each auto-increment pointer is sizeof (your data type)

Therefore, the value of arr + 1 will add 4 (because the pointer type of the element corresponding to the one-dimensional array is int, 4 bytes), So + 1 will find the next element address of the array, and * Get the value corresponding to the pointer address, but the value in the two-dimensional array is not int type, but int [3]

So arr is the pointer to the first row of a two-dimensional array, that is, the value of {1, 2, 3} in the corresponding array, so its size is 12 bytes.

 

At this time, we ran another program.

int arr[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };printf("\n%x", arr);//printf("\n%x", *arr);//

It is found that both values are the first address of the array, and the first is the pointer of the first line of the array, so it is normal to print the first address of the first line directly. We can understand that

But shouldn't the second line be printed?

It turns out that the whole row of data has no actual meaning. In this case, the compiler converts the pointer to the 0th elements of the row, just like the name of a one-dimensional array, the entire array is expressed when it is defined or used together with sizeof and &. When it appears in an expression, it is converted to a pointer pointing to the array's 0th elements. Therefore, if we enter ** arr, the value of the element in column 0th of the row is printed, that is, 1.

 

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.