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

后端 未结 5 1042
盖世英雄少女心
盖世英雄少女心 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:48

    If You define an array like

    char name[20];
    

    name is implicitly convertible to char*, but &name is of the type char (*)[20] (a pointer to an array of 20 characters). The addresses are the same.

    Check the address of (&name + 1). It differs form &name by the sizeof(char [20]).

提交回复
热议问题