Array pointer and pointer array, array pointer Array

Source: Internet
Author: User

Array pointer and pointer array, array pointer Array

Array pointer (also called row pointer)
Define int (* p) [n];
() A high priority indicates that p is a pointer pointing to an integer one-dimensional array. The length of this one-dimensional array is n, or the step size of p. That is to say, when p + 1 is executed, p must span the length of n integer data.

To assign a two-dimensional array to a pointer, assign a value as follows:
Int a [3] [4];
Int (* p) [4]; // This statement defines an array pointer pointing to a one-dimensional array containing four elements.
P = a; // assign the first address of the Two-dimensional array to p, that is, a [0] Or & a [0] [0].
P ++; // after the statement is executed, that is, p = p + 1; p spans Row a [0] [] pointing to row a [1] []

Therefore, an array pointer is also called a pointer to a one-dimensional array or a row pointer.

Pointer Array
Define int * p [n];
[] High priority. It is first combined with p to form an array, and then int * indicates that this is an integer pointer array, which has n Array elements of pointer type. The execution of p + 1 here is incorrect, so the value assignment is also incorrect: p = a; Because p is an unknown representation, only p [0], p [1], p [2]... p [n-1], and they are pointer variables that can be used to store variable addresses. But it can be like this * p = a; here * p represents the value of the first element of the pointer array and the value of the first address of.
To assign a two-dimensional array to a pointer array:
Int * p [3];
Int a [3] [4];
For (I = 0; I <3; I ++)
P [I] = a [I];
Int * p [3] indicates that three pointer variables exist in the memory of a one-dimensional array, namely p [0], p [1], and p [2].
Therefore, assign values respectively.

In this way, the difference between the two is suddenly clear. The array pointer is just a pointer variable. It seems that the C language is specifically used to point to a two-dimensional array, which occupies the storage space of a pointer in the memory. A pointer array contains multiple pointer variables in memory and occupies the storage space of multiple pointers.
It also needs to be noted that when it is used to point to a two-dimensional array, its reference is the same as the array name reference.
For example, to represent an element in column j of row I in the array:
* (P [I] + j), * (p + I) + j), (* (p + I )) [j], p [I] [j]


Array pointer, pointer array is different! To be detailed, super detailed! I have divided it many times, but it is still unclear

Int a [3] [4] is a two-dimensional array.
Int (* p) [4] is equivalent to int p [] [4]. It is a pointer to a two-dimensional array and can point to a two-dimensional array with the second dimension of 4. And a is such an array, so the following is legal.
P =;
Int * p [3] is a pointer array. To put it bluntly, we define three pointers: p [0], p [1], and p [2]. They can be used separately.
Int a1, a2, a3;
P [0] = & a1;
P [1] = & a2;
P [2] = & a3;

What are the differences between array pointers and pointer arrays?

1 There is no difference between the two forms, and all the pointers of the char * type are defined. However, in some books, we recommend that you use the * method close to the variable to avoid errors in the continuous declaration, for example: char * ptr, a; you can obviously know that one is a pointer variable and the other is a char variable.
2. A two-dimensional array can be declared as a pointer to a pointer. In this way, a double pointer can be used to accept the form parameter.
Void f (int ** a) // how to write?
{
}
Int a [100] [100];
F ();

3. the return value is a pointer, but the returned address must be the space applied for in the heap space or in the static zone. Do not return the address in the function stack to prevent stack space from being released and become a ghost memory when exiting the function.

Char * fun (){
Char * ptr = "abc ";
Return ptr;
}


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.