Pointers to pointers vs. normal pointers

前端 未结 12 1060
南旧
南旧 2021-01-30 08:33

The purpose of a pointer is to save the address of a specific variable. Then the memory structure of following code should look like:

int a = 5;
int *b = &a;         


        
12条回答
  •  感情败类
    2021-01-30 08:33

    In

    int a = 5;
    int *b = &a;   
    int *c = &b;
    

    You get a warning because &b is of type int **, and you try to initialize a variable of type int *. There's no implicit conversions between those two types, leading to the warning.

    To take the longer example you want to work, if we try to dereference f the compiler will give us an int, not a pointer that we can further dereference.

    Also note that on many systems int and int* are not the same size (e.g. a pointer may be 64 bits long and an int 32 bits long). If you dereference f and get an int, you lose half the value, and then you can't even cast it to a valid pointer.

提交回复
热议问题