Array name and pointer difference

In the beginning learn C language, one of the most commonly heard argument is "arrays and pointers are the same," unfortunately, this is a very dangerous statement is not entirely correct.

First look C standard for the interpretation of the pointer: a pointer is a variable, the variable value is the address of another variable. Well, since pointers are variables, the pointer must have their own storage space, but the value is within the storage space is an address value, rather than something else. According to the most commonly heard argument that "the name of the array and pointers are the same", then the name of the array should also have their own storage space for myself?

 

In fact, in the first graph, this time we compiled, the compiler usually being given, as shown. This time we look at the error message can know the results of an array of Natori address obtained is actually a: char (*) [4], represents the connotation of the name of the array is a data structure, that is: from an array the beginning of the start address to the array length and sizeof (char) the product of the width of a data structure (what I understand, we have a different view if the message can discuss together). C language provides that the first address array representatives of the array, which is the address No. 0 elements.

But an array of Natori address Shique to pay attention, when understanding the meaning of "array Natori address" this expression must remember: array name is "Array" variable names such variables, so that, & array like to understand , and it takes is the address of this variable "array."

In the second graph, we compile it, suggesting the error is not from "char (*) [4]" is converted to "char *", just visible pointer points to the first character string is the address of the first character.

 

★ Summary: Connotation array name is a data structure, that is to say: start address of the array, and the array length to sizeof (char) the product of the width of a data structure. Array name represents the first array address, note that this is the wording is "representative", the compiler simply assign the appropriate space for the array elements, not assigned to any other space, the array name is a variable name "array" of such variables just a symbol, the system does not allocate any space for it. Not allocated space, there are no variables, so it is not a pointer.

In addition, only when the array name used in an expression, the compiler will produce a constant pointer to it.

As well as in the function

#include <iostream>

 2 void arrayTest(char str[])

 3 {

 4   cout << sizeof(str) << endl;

 5 }

 6 int main ()

 7 {

 8   char str1[10] = "I Love U";

 9   arrayTest(str1);

10 return 0;

11 }

The output program is four. impossible? The connotation of the name of the array as an array of data structures, in arrayTest function in vivo, str is an array name, why the result is the length of sizeof pointer?

 This is because:

(1) as a function of parameter array name, function in vivo, it loses the meaning itself will degenerate into a pointer;

(2) Unfortunately, at the same time lose their meaning, when the name of the array passed as a parameter, just pass the address of an array of delegates to the function, the transfer process here is passed by value, this time in the body of the function to be adjusted to make the self-energizing (decrement) operation and the like, is the parameter increment (decrement), regardless of the name of the calling function in the array.

Guess you like

Origin blog.csdn.net/qq_29250265/article/details/94546510