C pointer (5) c array nature, Array

Source: Internet
Author: User

C pointer (5) c array nature, Array

(5) c array nature

Arrays in c are an aggregation type. Elements of the same type are stored in order, that is, arrays. In c, the array type is defined by nesting.

Look at the memory storage format of the next two-dimensional array int array [2] [3] = {1, 2, 3}, {4, 5, 6:


Arrays in c are nested and defined: Elements in a two-dimensional array are one-dimensional arrays, and elements in a three-dimensional array are two-dimensional arrays ......

A two-dimensional array like array [2] [3] is composed of two one-dimensional arrays: array [0], array [1], the size of the two one-dimensional arrays is the second subscript 3 of array. Array [0] and array [1] are two one-dimensional array names. Because the elements are stored sequentially, you can access the elements in the array in one of the following ways:

Int main (void) {int array [2] [3] = {1, 2, 3}, {4, 5, 6 }}; // point p to the first element of the array int * p = & array [0] [0]; while (p <& array [1] [3]) {printf ("% 4d", * p); p ++;} printf ("\ n"); return 0 ;}
Run


The key to successful access is that the multi-dimensional array in c is essentially an array and the elements are stored in sequence.

The & array [1] [3] appears in the Code. Is it out of bounds? In theory, array [1] [2] is the last element of array. array [1] [3] is the next element. Here we take its address instead of accessing the element, this is acceptable.


Next we will discuss the types of pointers for these three Arrays: array, array [0], and array [1.

First, in the (3) array and pointer, we mentioned that for the array int array [N]; (N is a constant ). Array name array is a constant pointer to the first element of the array (that is, it cannot point to another storage unit), and & array is a pointer to the entire array. And so on:

Lab Verification:
int main(void){int array[2][3] = {{1, 2, 3},{4, 5, 6}};int *pa = array[0];int (*pb)[3] = &array[0];int (*pc)[3] = array;int (*pd)[2][3] = &array;return 0;}

Compilation, without any warning, our conjecture is correct. The above two experiments fully illustrate the nature of the c array: Array.
Let's take a short test. The 3D array int array [2] [3] [4]; The type of array? & Array type? Array [0] type? Array [0] [0] type? (Think about the answer at the end, and then look at the answer .)
In another test, pointer operations are used for verification.
int main(void){int array[2][3] = {{1, 2, 3},{4, 5, 6}};int *pa = array[0];int (*pb)[3] = &array[0];int (*pc)[3] = array;int (*pd)[2][3] = &array;printf("array...%p\n", array);printf(" pa+1...%p\n", pa + 1);printf(" pb+1...%p\n", pb + 1);printf(" pc+1...%p\n", pc + 1);printf(" pd+1...%p\n", pd + 1);return 0;}
Run
Calculate the difference between the last four and array, and draw a conclusion.
To sum up, the essence of array c is the array of arrays. For the array name array and the pointer type of & array, you only need to remember two points. These two points are:
Test answer: the array type is int (*) [3] [4]; & the array type is int (*) [2] [3] [4]; the type of array [0] is int (*) [4]; The type of array [0] [0] is int *.
Column Directory: C pointer


The essence of accessing array elements using pointers and subscripts in C language is different.

The two mean the same, but the compiler does not process the same. Just like the example above, a [2] and * (a + 2) mean one thing. But for the compiler, a [2] is a fixed method, so there is a fixed way to handle address offset or something, and * (a + 2) in terms of syntax, we should calculate a + 2 first and then take the value. If the compiler is not optimized and compiled according to this definition, we will calculate the address a + 2 first, therefore, the efficiency is lower. It is hard to say whether compilation optimization can make the two consistent.
For example, I compiled such a program.
Int a [100];
Int B;
Int I, j, k;
For (I = 0; I <1000000; I ++ ){
For (j = 0; j <100; j ++ ){
For (k = 0; k <100; k ++ ){
B = a [0];
}
}
}
It is the same as changing B = a [0] To B = * (. In contrast, changing to B = a [k] and B = * (a + k) significantly increase the time consumed by the latter, but the former is also increased, but less. This statement cannot be used to test Compiler Optimization because the entire process is optimized.

What is the pointer array in C language? I am confused about this concept. The definition and function of the pointer array are as follows:

I used to be confused. It's easy to understand it based on a normal array.
For example, int a [10] is called an int variable in the int array.
So int * a [10] is called int * array int * to indicate a pointer, so it is called a pointer array! That is, the array containing int * type variables

Unclear question

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.