简体   繁体   中英

scanf() does not read input string when first string of earlier defined array of strings in null

I defined an array for strings. It works fine if I define it in such a way the first element is not an empty string. When its an empty string, the next scanf() for the other string stops reading the input string and program stops execution.

Now I don't understand how can defining the array of strings affect reading of input by scanf() .

  char *str_arr[] = {"","abc","","","b","c","","",""}; // if first element is "abc" instead of "" then works fine

  int size = sizeof(str_arr)/sizeof(str_arr[0]);

  int i;

  printf("give string to be found %d\n",size);


  char *str;
  scanf("%s",str);
  printf("OK\n");

Declaring a pointer does not allocate a buffer for it in memory and does not initialize it, so you are trying to dereference an uninitialized pointer ( str ) which results in an undefined behavior.

Note that scanf will cause a potential buffer overflow if not used carefully when reading strings. I recommend you read this page for some ideas on how to avoid it.

Actually, you are getting it wrong my brother. The initialization of str_arr doesn't affect the working of scanf() , it may however seem to you like that but it ain't actually. As described in other answers too this is called undefined behavior . An undefined behavior in C itself is very vaguely defined .

The C FAQ defines “undefined behavior” like this:

Anything at all can happen; the Standard imposes no requirements. The program may fail to compile, or it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended.

It basically means anything can happen. When you do it like this :

char *str;
  scanf("%s",str);

Its an UB. Sometimes you get results which you are not supposed to and you think its working.That's where debuggers come in handy.Use them almost every time, especially in the beginning. Other recommendation wrt your program:

  • Instead of scanf() use fgets() to read strings. If you want to use scanf then use it like scanf("%ws",name); where name is character array and w is the field width.
  • Compile using -Wall option to get all the warnings, if you would have used it, you might have got the warning that you are using str uninitialized .

Go on reading THIS ARTICLE , it has sufficient information to clear your doubts.

You are passing to scanf a pointer that is not initialized to anything particular, so scanf will try to write the characters provided by the user in some random memory location; whether this results in a crash or something else depends mostly by luck (and by how the compiler decides to set up the stack, that we may also see as "luck"). Technically, that's called "undefined behavior" - ie as far as the C standard is concerned, anything can happen.

To fix your problem, you have to pass to scanf a buffer big enough for the string you plan to receive:

char str[101];
scanf("%100s",str); /* the "100" bit tells to scanf to avoid reading more than 100  chars, which would result in a buffer overflow */
printf("OK\n");

And remember that char * in C is not the equivalent of string in other languages - char * is just a pointer to char , that knows nothing about allocation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM