“Address of” (&) an array / address of being ignored be gcc?

后端 未结 5 1044
盖世英雄少女心
盖世英雄少女心 2020-12-05 19:00

I am a teaching assistant of a introductory programming course, and some students made this type of error:

char name[20];
scanf(\"%s\",&name);

5条回答
  •  死守一世寂寞
    2020-12-05 19:42

    In your example, the array test is a block of 50 ints. So it looks like this:

    | int | int | ... | int |
    

    When you apply the unary & operator to an array, you get the address of the array. Just like when you apply it to anything else, really. So &test is a pointer that points to that block of 50 ints:

    (&test) -----------> | int | int | ... | int |
    

    A pointer that points to an array of 50 ints has type int (*)[50] - that's the type of &test.

    When you just use the name test in any place where it's not the operand of either the sizeof or unary-& operators, it is evaluated to a pointer to its first element. So the test that you pass to foo() evaluates to a pointer to the test[0] element:

    (test) -----------------\
                            v
    (&test) -----------> | int | int | ... | int |
    

    You can see that these both are pointing to the same address - although &test is pointing to the whole array, and test is pointing to the first element of the array (which only shows up in the different types that those values have).

提交回复
热议问题