There is a question about the address of the array in "CPP Primer Plus".

in the declaration statement short tell [10];, tell points to the first element of the array, indicating the address of a 2-byte block of memory, and & tell points to the entire array, indicating the address of a 20-byte block of memory. So far, I understand, but there are a few things I don"t understand. I"d like to ask the bosses for advice:

    The
  1. book says that the expression tell+1 increases the value of tell by 2, and the expression & tell+2 increases the value of & tell by 20. I don"t understand & why would tell+2 increase the value of & tell by 20? Doesn"t & tell represent the address of the entire array? the value of & tell should be increased by 40 after & tell+2. If it is & tell+1, it will increase by 20, right?
  2. you can think of int * as the type of pointer to int; but what about short (*) [20]?
CPP
Mar.29,2021

there may be something wrong in the book. There are also inaccuracies in your understanding. In fact, you can get it by yourself.

tell is used alone, referring to the entire array. However, when placed in some expressions, it is implicitly converted to a pointer to the first element.

cout << sizeof(tell) << endl; // 20
cout << sizeof(tell + 1) << endl; // 483264
cout << tell << endl; // 
cout << tell + 1 << endl; // 21short

& tell is not in suspense, it refers to a pointer to the entire array.

cout << &tell << endl; // 
cout << &tell + 2 << endl; // 40

as for short (*) [20] , I've never seen it written like this.


add the answer upstairs
short (*) [20] is the pointer of short [20] , is an array pointer

Menu