Input with char pointer vs. char array

前端 未结 6 1960
不思量自难忘°
不思量自难忘° 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:12

    In the first example a is a "dangling pointer" - it's a pointer that contains the address of a somewhat random memory location. In most cases accessing this address will result in a crash. Bottom line: you need to allocate memory for a.

提交回复
热议问题