Two-dimensional array, pointer array and array pointer

Source: Internet
Author: User
Two-dimensional array, pointer array and array pointer

Http://www.cnblogs.com/birduu/archive/2013/04/04/2999185.html

I. pointer array and two-dimensional array
Let's define a struct pointer array P: char * P [3]; The Memory diagram after definition is as follows:

For the two-dimensional array char birduu [3] [16] = {"birduu.com", "course textbook", "free, the value of birduu [0] is the first address of the string "birduu.com". That is to say, birduu [0] can be interpreted as a struct pointer pointing to the first element 'B' of the string "birduu.com. For pointer array p, p [0] is a struct pointer, and the base type of P [0] Is struct. It is the same as the base type of birduu [0, the value can be assigned as follows: P [0] = birduu [0];
In this case, P [0] points to the string "birduu.com ". Similarly, execute P [1] = birduu [1]; P [2] = birduu [2]; then P [1] points to the string "course textbook ", P [2] points to the string "free ". The memory is as follows:

Then, you can use P to operate the two-dimensional array birduu, as shown in the example column.
Note that p is a pointer array, and the array name cannot be assigned values. Do not write it as: P = birduu;

Program instance:

# Include <stdio. h>
Void main ()
{
Char birduu [3] [16] = {"birduu.com", "course textbook", "free "};
Char * P [3];
P [0] = birduu [0];
P [1] = birduu [1];
P [2] = birduu [2];

Printf ("% s \ n", P [0]);
Printf ("% C \ n", P [0] [1]);
Printf ("% C \ n", * (p + 2) + 3 ));
}

Output: birduu.com
I
E

 

2. array pointer and two-dimensional array

An array pointer is a pointer to an array. First, define an array pointer Variable P: Char (* P) [16];
* P indicates that p is a pointer variable. Char [16] indicates that the base type of P is a one-dimensional array containing 16 struct elements, that is to say, P can point to a one-dimensional array containing 16 struct elements.
Next let's take a look at how to use an array pointer to operate a two-dimensional array. As we mentioned in the http://birduu.com/260.html section, we can understand the two-dimensional array name as a pointer variable pointing to the first row array element. Two-dimensional array
Char birduu [3] [16] = {"birduu.com", "course textbook", "free"}; the first row array element is a one-dimensional array containing 16 struct elements, therefore, we can understand the two-dimensional array name birduu as a pointer to the one-dimensional array birduu [0]. Its base type is a one-dimensional array containing 16 struct elements, the value of birduu is the address of the one-dimensional array birduu [0.
From the above analysis, we can see that the base types of the pointers birduu and P are the same, so we can assign a value like this: P = birduu; then P points to birduu [0], because the base type of P is a one-dimensional array containing 16 struct elements, the pointer (p + 1) points to birduu [1], and the pointer (p + 2) points to birduu [2]. The memory is as follows:

 

Since then, P can be used to operate the two-dimensional array birduu. Since P is a pointer variable, it can reference array elements regardless of the pointer, subscript, or hybrid method.

Program instance:

# Include <stdio. h>

Void main ()
{
Char birduu [3] [16] = {"birduu.com", "course textbook", "free "};
Char (* P) [16];

P = birduu;

Printf ("% s \ n", P [0]);
Printf ("% C \ n", P [0] [1]);
Printf ("% C \ n", * (p + 2) + 3 ));
Printf ("% C \ n", * (p [2] + 3 ));
Printf ("% s \ n", * (p + 1 ));
}

Output:

Birduu.com
I
E
E
Course textbook

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.