开发者

C++ - The name of the array and the starting address

In the C++ Without Fear: A Beginner's Guide That Makes You Feel Smart in Chapter (7), it mentions t开发者_C百科he following:

char str[10] = "Hello!";

This declaration creates the array shown, and associates the starting address with str. (Remember that the name of an array always translates into its starting address.)

What is meant by this? I mean, the association of the starting address with str?

Thanks.


It means that 'str' can be treated as a starting address where the "Hello" string is stored.

Consequently str + 1 is the address where the second character of the string is stored.

  |   str   | str+1  | str+2  | str+3 | str+4 | str+5  |
  |  str[0] | str[1] | str[2] | str[3]| str[4]| str[5] |
  |    H    |  e     |   l    |   l   |   o   |  \0    |


What is meant by this?

This declaration creates the array shown, and associates the starting address with str.

This means that

  1. an array is constructed in memory and filled with the contents of "Hello!" (that is, the characters 'H', 'e', 'l', 'l', 'o', '!', '\0')
  2. a pointer variable str is created and points to the starting address of the array created before.

However, this is wrong. The description in the book is wrong, or at least seriously misleading.

str is not a pointer, it’s an array. Consequently, it’s not “associated with the starting address”. It’s associated with the whole array.

Unfortunately, the issue gets even more confusing because C++ and C allow arrays to be implicitly converted to pointers. That is, the following is valid:

char* x = str;

This is called “pointer decay” and it happens all the time in C++. It’s enough to stare pointedly at an array to convert it to a pointer.

This pointer decay is equivalent to the following:

char* y = &str[0];

That is, when an array decays to a pointer, that pointer points to the first element of the array. This is probably meant by “associates the starting address with str”, but as I’ve explained above this isn’t really correct. (Furthermore, the starting address of an array is usually before the address to its first element, since an array also needs to store its size and that size is usually put into memory immediately before the first element of the array; but this behaviour cannot be relied upon, it’s an implementation detail.)


str can be viewed as a pointer to the address of the first element of the array, that is to &str[0].


First of all, the statement "the name of an array always translates into its starting address.", is not 100% correct.
The name of array translates into its starting address i.e. address of its first element except in two cases

  1. When it is used as an operand of sizeof operator.
  2. When it is used as an operand of address of i.e. & operator.

For ex :

char str[10]="Happy";
std::cout<<sizeof(str); // prints 10 as size of whole array is 10 bytes
std::cout<<&str; // address of array of 10 char, not address of address of char  

In all other cases, array name translates into pointer to first element. So For ex :

char str[10]="Happy";
std::cout<<str; // same as &str[0]. Let it prints 0x1000
std::cout<<str+1; // same as &str[0]+1, will print 0x1001
std::cout<<&str; // Let it prints 0x1000
std::cout<<&str+1 // will print 0x100A


It means that str, taken alone, is of can be converted to type char* (pointer to character), so it actually contains the memory starting address of the character array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜