Arrays and pointers, originally did not want to write, think this part is almost, but I wrote the program when still found a mistake. First of all, let me ask:
Pass a two-dimensional array to a function, and then I want to calculate the number of rows in the array for this function.
Write a similar error demo code to get up:
#include <stdio.h>#include<stdlib.h>voidFuncinta[][3]) {printf ("%p\n", a); printf ("%p\n", a[0]); printf ("%p\n", &a); printf ("%d\n",sizeof(a)); printf ("%d\n",sizeof(a[0])); printf ("%d\n",sizeof(a)); printf ("%d\n",sizeof(a)/sizeof(a[0])); printf ("%d\n", a[1][2]);}intMain () {inta[2][3] = {{1,2,3},{4,5,6}}; printf ("%d\n",sizeof(a)); printf ("%d\n",sizeof(a[0])); printf ("%d\n",sizeof(a)/sizeof(a[0])); Func (a); System ("Pause"); return 0;}
The array name itself is an address constant , but in some special cases its semantics can change. For example, sizeof (a), at this point a represents the entire array object (this refers to a syntax object, not an instance of a class) and not the constant itself. Based on this semantics, the address of the array name is also legal, for the &a of the result of arrays A is equal to the value of the address constant itself. This is a compromise on the principle that the C + + Standard committee can always take addresses in order to maintain the syntax object A as an Lvalue (L-value).
#include <stdio.h>int Main (void char str[] = " world " char * pstr = " world ; printf ( %d%d ", sizeof (str), sizeof (PSTR)); GetChar (); return 0 ;}
Running Results 6 4.
Explain:
Char str[] = "World";
This initializes the unqualified length, and "World" contains the Terminator ' 6 characters, so the length of the init str is 6, and because each element in the char array (char variable) occupies 1 bytes of space, the size of the str[] array is 6 bytes.
Char *pstr = "World";
Since PSTR is a pointer, regardless of whether it points to a string, what string it points to, sizeof (PSTR) equals sizeof (int), 32-bit platform equals 4.
Causes of differences:
Here char str[] = "world"; declares and defines an array str[] (of course, the syntax of the C language does not allow the entire array to be referenced outside of the definition, the following notation is only to differentiate semantics), then the identifier STR has the double semantics: first, the type is char* The const address constant whose value is equal to the address of the first element in the array, that is, Str is equivalent to (char* const) &str[0]; second is the syntax object that represents the entire str[] array. in sizeof (str), the meaning of STR is str[], so the size of the entire array is returned (this size is determined in the previous array definition), and pstr is just a pointer, sizeof (PSTR) You can only return the number of bytes that the pointer itself occupies and not determine the size of the space allocated for the content it points to.
(Note that the address constants are not pointers, different types!) Although the address constants can degenerate into corresponding pointers during the parameter passing of the function. Here the LZ and 2L are obviously due to this erroneous understanding resulting in an incorrect evaluation of the sizeof () result of the logarithm group. )
About the semantics of array names and the "array name actually represents a pointer (
The wrong!!! ) "For reasons that need attention later on ...
A brief talk on pointers and arrays in C (vi)