Explanation of C language Array
This article mainly introduces the C language array. If you need it, please refer to it.
What is an array name?
An array is a continuous memory available.
For example, declare an int array.
Int array [] = {1, 2, 3 };
What does array represent? Some documents say that the array name is a constant pointer to the first address of the array.
Next we can verify it.
I know that the sizeof operator can return the memory bytes occupied by an object or type.
For example:
Int I = 1;
The result of sizeof (I) is 4 (Some compilers under 64-bit machines are 8)
Then we print sizeof (array)
Printf ("% dn", sizeof (array ));
The result is 12.
But we all know that sizeof (pointer variable) = 4.
We can conclude that the array name is not completely a constant pointer to the first address of the array.
Why is it incomplete? Because when we use arrays to access array elements. It becomes like a constant pointer.
For example
Array [0] is equivalent to * (array + 0)
At this time, array is a constant pointer to the first address of the array. the pointer type is a pointer to the array element type. Here is the int * type.
We can understand this as follows:
The name of a university is array.
Someone asks you what array is. You will tell him that array is a university, the size of the area, and so on.
However, if someone asks you how to get to array, you will tell him where the school gate (first address) of array is.
Conclusion: The array name represents a memory area, but it becomes a constant pointer to the first address of the array.
But there is also a small trap:
The Code is as follows:
# Include <stdio. h>
Void foo (int a [])
{
Printf ("% dn", sizeof ());
}
Int main (void)
{
Int array [] = {1, 2, 3 };
Foo (array );
Return 0;
}
The output is not 12, but 4.
For efficiency, the input parameter in an array references the input parameter instead of copying the input parameter. Because the length of the array may be large, copying one copy consumes too much resources.
Although this is my function
The Code is as follows:
Void foo (int a [])
{
Printf ("% dn", sizeof ());
}
This is what the compiler sees.
The Code is as follows:
Void foo (int *)
{
Printf ("% dn", sizeof ());
}
So sizeof (a) is sizeof (pointer variable) must be 4;
2-character array
First, let's look at a simple program.
The Code is as follows:
# Include <stdio. h>
Int main (void)
{
Char * str1 = "abc ";
Char str2 [] = "def ";
Printf ("% sn", str1 + 4 );
Return 0;
}
The output result is def.
As long as "xxxxx" is used in C, the system automatically adds the content of double quotation marks to the character constant area.
Note: printf ("xxxx") does not add "xxxx" to the character constant area.
The Code is as follows:
Char * str1 = "abc"; // adds abc to the character constant area, and assigns the first address to the str pointer variable.
Char str2 [] = "def"; // Add def to the character constant area, and add a character array to the function stack is also def. str2 points to the array in the stack.
Char str [] = {'x', 'y', 'z'}; // only the array is added to the function stack.
Since the character constant area is continuous
Printf ("% sn", str1 + 4 );
The str2 value can be printed.
Three-dimensional array
Int array [] [3] = {1, 2, 3, 4, 5, 6 };
As we have mentioned above, when using array to access an element, array is a pointer type pointing to an array element and pointing to the first address of the array.
The elements of a two-dimensional array are arrays,
This is easier to understand:
Int array [] [3] = {1, 2, 3}, {4, 5, 6 }};
All can think of array as this.
Int (* const array) [3];
When I access array elements
Array [x] [y] is * (array + x) + y) in the compiler's opinion)
* (Array + x) is an array of the row x type as "int [3]" (not written in C,
The array name is used as the first address pointer when accessing the element. Here * (array + x) is equivalent to the array name,
Pointer type int *, pointing to the address array + sizeof (int (*) [3]) * x.
* (Array + x) + y) is used to access the y element of the array ).
These are my understanding of the C language array. If it is incorrect, thank you for your correction.