What is the nature of the array name in C and the array name is a pointer?
1, the array name is the first address of the array element
#include <stdlib.h>
#include <stdio.h>
void Main ()
{
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printf ("a:%d &a:%d:%d \ n", A, &a);
System ("pause");
}
In the above program, a &a is the same size, proving that a is the first address of an array element;
2, the array name is not a pointer, see example:
#include <stdlib.h>
#include <stdio.h>
void Main ()
{
Char str[10];
char *pstr = str;
printf ("sizeof (str):%d sizeof (PSTR):%d:%d \ n", sizeof (str), sizeof (PSTR));
System ("pause");
}
In the above program, the result of the operation is 10 and 4.
Conclusion: If the array name is a pointer, then the value of STR should be 4, but the result is the size of the entire array 10;
is not a pointer, why is STR able to assign a value to a pointer?
3. The array name is not a pointer to the pointer
#include <string.h>
#include <iostream.h>
int main (int argc, char* argv[])
{
Char str1[10] = "I love U";
Char str2[10];
strcpy (STR2,STR1);
cout << "string array 1:" << str1 << Endl;
cout << "string array 2:" << str2 << Endl;
return 0;
}
In the above example, the strcpy function is a pointer to the char type in the C-language function library, and str1 and str2 are considered
Pointers with.
Conclusion: The array name represents a data structure, that is, an array, the array name can be converted to pointers to the array, and is a constant refers to the
Needle.
4. The array name can be used as a pointer constant
int a[10];
If you perform a a++ operation on a, the program compiles an error because although the array name can be converted to a pointer to its reference entity, it
Can only be viewed as a pointer constant and cannot be modified.
5, the array name as a function parameter will degenerate into a pointer
#include <iostream>
void Arraytest (char str[])
{
cout << sizeof (str) << Endl;
}
int main ()
{
Char str1[10] = "I love U";
Arraytest (STR1);
return 0;
}
In the above program, the output is 4.
Conclusion: When the array name is a function parameter, in the function body, it loses its own connotation, just a pointer, and in the letter
The number of bodies can be self-increment, self-reduction and other operations, can be modified.
C + + compilers such as Char Buf[30],int a[10] that appear in formal parameters will take it as pointers, and will not be actively allocated more
Memory, which is automatically optimized by the C + + compiler.
6. Array name extension:
#include <stdlib.h>
#include <stdio.h>
void Main ()
{
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printf ("a:%d &a:%d:%d \ n", A, &a);
printf ("a+1:%d &a+1:%d \ n", a+1, &a +1);
}
The above example a is the same size as the &a, stating that A is the address of the first element of the array, and that a+1 and &a +1 are not the same size, which is essentially the data type
Different, that is, the step of the pointer is not the same;
Char A[20];a represents the first address of a character array element, with a type of char *, with a step of 1;&a representing the first place of a character array
Type char[], with a step of 20;
Char B[10][30];b represents the first address of a two-dimensional array element, which is a pointer of type char (*A) [30], with a step of 30;&b and two
The first address of a dimension array element, type char[10][30], with a step of 300;
The nature of array names in C language