Segmentation fault while taking string from user

后端 未结 4 1038
说谎
说谎 2021-01-29 13:07

Code:

int main()
{
  char *name=NULL;
  int n;
  printf(\"\\nenter the string\\n\");
  scanf(\"%s\",name);
  n=strlen(name);
  printf(\"%d\",n);
  return 0;
}
         


        
4条回答
  •  再見小時候
    2021-01-29 13:46

    You didn't allocate any memory for pointer to char name.

    Example:

    char * name = malloc( sizeof( char ) * MAX_STRING_LENGTH ) ;
    

提交回复
热议问题