Issue with pointer to character array C++

前端 未结 6 856
醉梦人生
醉梦人生 2020-12-11 03:10

I have what I thought should be a very simple snippet of code to write, though I\'m unable to compile for a reason which I do not understand.

The following simplifie

6条回答
  •  有刺的猬
    2020-12-11 03:21

    • The declaration char buffer[9] = "12345678"; creates an array of 9 chars.
      Here, buffer is the address of its first element but not of the array.

    • char* pBuffer = buffer; is a correct expression as pBuffer is a pointer to char and can address the first element.

    • But the expression char* pBuffer = &buffer is wrong, because pBuffer can't address an array. (error in your code, &buffer address of array as explained below)

    Difference between buffer and &buffer

    &buffer means address of array. The values of buffer and &buffer are really the same, but semantically both are different. One is an address of a char, while the other is an address of an array of 9 chars.

    buffer[9] = "12345678";
    
    
    +----+----+----+---+---+----+----+----+---+----+ 
    | '1'| '2' |'3'|'4'|'5'| '6'| '7'|'8' | 0 |  ...........
    +----+----+----+---+---+----+----+----+---+---+----+  
     201   202  203 204 205 206  207   208 209 210  211
      ^     ^                                         
      |     |                                         
    (buffer) (buffer + 1)                                         
    |                                         |
    |-----------------------------------------|--------
    |201                                      | 210
      ^                                          ^
      |                                          |
    (&buffer)                                 (&buffer + 1)     
    

    I used decimal numbers for address instead of hexadecimal

    Even though the value of buffer is 201 and the value of &buffer is 201, their meaning is different:

    • buffer: first element's address—its type is char*.
    • &buffer: complete char array's address—its type is char(*)[9].

    Additionally, to observe the difference, add 1 :

    buffer + 1 gives 202 that is the address of the second array element '2' but
    &buffer + 1 gives 210 which is the address of the next array.

    On My System, I write following code:

    int main(){
       char buffer[9] = "12345678";
       char (*pBuffer)[9] =  &buffer; 
    
       printf("\n %p, %p\n",buffer, buffer+1);
       printf("\n %p, %p\n",(&buffer), (&buffer+1));
    }  
    

    And the output is as below:

    0xbfdc0343, 0xbfdc0344
    
    0xbfdc0343, 0xbfdc034c
    

    [ANSWER]

    That's the reason Error is:

    error: cannot convert 'char ()[9]' to 'char' in initialization

    You are trying to assign 'char (*)[9]' type value to char*.

    • char (*ptr2)[9]; Here ptr2 is pointer to an array of 9 chars, And this time
      ptr2=&buffer is a valid expression.

    How to correct your code?

    As in Nate Chandler's answer:

    char buffer[9] = "12345678";
    char* pBuffer =   buffer;   
    

    or another approach,

    char buffer[9] = "12345678";
    char (*pBuffer)[9] =  &buffer;       
    

    Which you choose depends on what you need.

提交回复
热议问题