What is an array name
An array is a contiguous amount of memory available.
Like declaring an int array
int array[]={1,2,3};
What does the array represent? Some data say that an array name is a constant pointer to the first address of an array.
Here we can verify.
I know that the sizeof operator can return the number of bytes of memory that an object or type occupies.
Such as:
int i=1;
So the result of sizeof (i) is 4 (part of the compiler under the 64-bit machine is 8)
So we print sizeof (array)
printf ("%d\n", sizeof (array));
The result: 12.
But we all know sizeof (pointer variable) ==4.
All we can conclude is that the array name is not exactly a constant pointer to the first address of the array .
Why use incomplete, because we use arrays to access array elements. It becomes like a constant pointer again.
Like what
Array[0] is equivalent to * (array+0)
The array is a constant pointer to the first address of the array, and the pointer type is a pointer to the element type of the array. This is the int* type.
We can understand this:
The name of a university is called array.
Someone asked you what array is. You will tell him that the array is the university, the area and so on.
But when someone asks you how to get to array, you tell him where the school gate (first address) of the array is.
Conclusion: The array name actually represents an area of memory, but when used, it becomes a constant pointer to the first address of the array.
But here's a little trap:
Copy Code code as follows:
#include <stdio.h>
void foo (int a[])
{
printf ("%d\n", sizeof (a));
}
int main (void)
{
int array[]={1,2,3};
Foo (array);
return 0;
}
The output is not 12, but 4.
For efficiency reasons, an array pass is a reference to a parameter rather than a copy. Because the array length can be very large, a copy of the words too much resources.
Although I am so the function is this
Copy Code code as follows:
void foo (int a[])
{
printf ("%d\n", sizeof (a));
}
The compiler's eyes are like this.
Copy Code code as follows:
void foo (int *a)
{
printf ("%d\n", sizeof (a));
}
So sizeof (a) is sizeof (pointer variable) is definitely 4;
Two-character array
First, let's look at a simple program
Copy Code code as follows:
# include <stdio.h>
int main (void)
{
Char *str1= "ABC";
Char str2[]= "DEF";
printf ("%s\n", str1+4);
return 0;
}
The result of the output is def.
We need to know the C language as long as the use of "xxxxx", the system will automatically add double quotes to the Word constants volume area.
Note: printf ("xxxx") does not add "xxxx" to the character constants area.
Copy Code code as follows:
Char *str1= "ABC"; Adds abc\0 to the character constants and assigns the first address to the STR pointer variable.
Char str2[]= "DEF"; Adds def\0 to the character constants, and a character array is added to the function stack def\0,str2 points to an array in the stack.
Char str[]={' x ', ' y ', ' z '}; Only add an array to the function stack
Because the character constants is continuous, so
printf ("%s\n", str1+4);
You can print out the STR2 value.
32-D Array
int array[][3]={1,2,3,4,5,6};
As we've already said, when you use an array to access an element, the array is a pointer type that points to an array element, and a pointer to the first address of the array.
The elements of a two-dimensional array are arrays,
It is easier to understand this writing:
int array[][3]={{1,2,3},{4,5,6}};
All can think so that the array is so thought
int (*const array) [3];
When I access the array elements
Array[x][y] In the compiler appears to be * (* (array+x) +y)
* (array+x) gets an array of x row Type "int[3]" (C language does not have such a notation),
The array name is used as the first address pointer when accessing the element, where * (array+x) is equivalent to an array name,
pointer type int *, pointing to the address array+sizeof (int (*) [3]) *x.
When you access the first Y element of this array, you use the * (* (array+x) +y).
These are my understanding of the C language array, if the wrong place, thank you, light spray ha.