Pointer knowledge sorting 10-pointer to array

Source: Internet
Author: User

1. pointer to array

1. What is pointed to: we usually say that the pointer variable points to a variable or memory, which means that the pointer variable stores the address of a variable or the address of a memory block.

2. A variable can occupy multiple bytes. We usually say that an address refers to the starting address of the memory. For example, if the four bytes of int A and variable a stack are 0x10 0x11 0x12 0x13, the address of a is 0x10.

3. (1) When we previously defined an array, we usually think of this array as a set of variables of the same type, that is, each element of the array, the usage is a [I].

(2) We can also regard the array as a whole. int A [5], A is a variable, and the type of this variable is array, which occupies 40 bytes ,.

Define the basic model type name according to the variable. If the array is defined in this way, the overall concept may be better understood. Int [10] A, but the C language syntax is not written in this way.

(3) Get the address of a variable with the & Symbol. When we regard array A as a variable, its address is &.

If you want to store the & A data, you need the corresponding pointer variable, that is, the pointer to the array.

The definition method is as follows: int (* P) [5]. Actually, it is defined according to the type * P model. Int [5] * P is better understood, however, father of C language does not set this rule when creating syntax.

So P = &;

For P, the p + I address offset I * sizeof ()

(4) The following must be clearly divided:

For int A [5];

The difference between & A and & A [0] Is that the two obtained addresses are equal in numbers, but their data types are different.

Int * P1 = & A [0];

INT (* P2) [10];

P2 = &;

For example, we consider arrays as collections and as a whole.

P1 stores the address of a [0], that is, the starting address of a [0], 0x10.

P2 stores the address of a, that is, the actual address of the array, which is also the starting address of a [0] 0x10.

The P1 + 1 result is 0x14.

P2 + 1 returns 0x24

4. Usage of typedef

Typedef int (type *) [5];

Type is a type, which is a pointer type pointing to a one-dimensional array (which is more specific). The following statement defines that the variable P1 is equivalent.

Type * P1;

INT (* P1) [5];

5. Use at the syntax level

Typedef int (type *) [5];

Int A [5];

Type P;

P = & A; // here, the array name does not represent the address of a [0.

Then * P is.

A [I] ---> (* P) [I]; // note the priority here.


2. array name of a two-dimensional array

Int A [2] [3];

A two-dimensional array can be regarded as a one-dimensional array. A [0] a [1] is two elements of the array, and each element is a one-dimensional array (INT [3]).

The array name of a one-dimensional array can represent the address of the first element. The first element is an array of int [3] type.

Typedef int (* P) [3]; int A [2] [3]; P = A; P = & A [0]; for (I = 0; I <2; I ++) {for (j = 0; j <3; j ++) {// The following methods are equivalent to a [I] [J] = 1; * (p + I) + J) = 1; * (p [I] + J) = 1; p [I] [J] = 1 ;}}












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.