Input with char pointer vs. char array

前端 未结 6 1947
不思量自难忘°
不思量自难忘° 2021-01-03 05:46

consider the code

#include
int main(void)
{
   char* a;
   scanf(\"%s\",a);//&a and &a[0] give same results-crashes 
   printf(\"%s\",         


        
6条回答
  •  庸人自扰
    2021-01-03 06:13

    scanf needs a place where to put the characters it reads. You can pass it a pointer to a character array (what you do in your second example), or a pointer to some memory obtained in another way (say malloc()). In your first example, you pass it an uninitialized pointer, thus the problems you are observing.

提交回复
热议问题