Pointer array Definition
If all elements in the array are pointer types, we call this a pointer array. The pointer array is defined as follows:
Type name * array name [constant expression]
We used to learn how the elements of an array are of the char type. This is a char type array and how the array elements are of the int type. This is called an int type array, here, if the array elements are all pointers, This is the pointer array.
No, the pointer array is nothing special, but its elements are pointers. So when we use it, it is not much different from other integer arrays.
Example: int * x [10]
Through the previous explanation, it is easy for me to return to this representation. x is an array with 10 elements. Each element is a pointer pointing to an integer. Hey! Isn't it easy! Put the pointer in the array! It's like the potato programming pointer on the "string of Bamboo. A pointer is an abstraction used in a computer to indicate an address plus its interpretation method.
Now let's think about a problem. What is the length of this int x [10! Smart, you think of it first, pointer! The 32-bit system contains 4 bytes, so there are 10, that is, 40 bytes!
Main Applications of pointer Arrays
From its definition, we know that the pointer array is mainly used to manage pointers of the same type. In fact, the most common thing is to manage strings.
Now, we still have the old method. Let's first look at the memory format of the pointer array, so that we can get a clearer idea after applying it!
Do you think pointer arrays are fun! 3 4-byte pointers in the aString array !. You must pay attention to the preceding and char aString [] = "............ "This form is different. This form places strings in an array! In the pointer array, only the pointer is put in the number group! Is the pointer to the string, that is, the first address of the string. Now let's see where the real string is. Hey hey, you will know the address as soon as you read it. The address of the first string is 00422080, and then it will be placed in sequence. Okay. Let's go to the address and see if this is the case!
Haha! You can see it! All records are pointer in the array!
A necessary comparison
Let's take a closer look at the example below:
# Include void main () {char * aStringA [] = {"Im not afraid to take a stand", "Well walk this road together, through the storm", "Whatever weather, cold or warm "}; char aStringB [] [60] = {" Im not afraid to take a stand "," Well walk this road together, through the storm ", "Whatever weather, cold or warm "};}
What about this example! How to output. The two results are the same! But what are their differences ?! Let's take a good look at my previous notes and summarize the differences below:
1. The first array string is in the constant area, and the second array string is in the stack area.
2. The length of the first array is 12 bytes, and that of the second array is 180 bytes.
3. The first array is like a variable-length array of advanced languages. The second array is equivalent to a fixed-length array with fast addressing capabilities.
4. It is very convenient to exchange elements in the first array. You only need to exchange pointers. If the second array is exchanged, the entire element must be copied and then exchanged.