scanf()函数不读取输入字符串当空字符串的早期定义的数组的第一个字符串 [英] scanf() does not read input string when first string of earlier defined array of strings in null

查看:294
本文介绍了scanf()函数不读取输入字符串当空字符串的早期定义的数组的第一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义字符串数组。它工作正常,如果我以这样的方式定义它的第一个元素是不是空字符串。当它是一个空字符串,接下来的 scanf()的为其他字符串停止读取输入字符串并计划停止执行。

现在我不明白如何通过 scanf函数定义字符串,影响读取输入的数组()

 的char * str_arr [] = {,ABC,,,B,C,,,}; //如果第一个元素是不是ABC然后工作正常  INT大小= sizeof的(str_arr)/ sizeof的(str_arr [0]);  INT I;  的printf(给字符串被发现%d个\\ N,大小);
  字符*海峡;
  scanf函数(%S,STR);
  的printf(OK \\ n);


解决方案

其实,你得到它错了我的兄弟。初始化 str_arr 不影响的工作scanf()的,它可能不过似乎你这样的,但它实际上不是。正如在其他的答案中描述过这个叫未定义行为即可。一个的在C本身未定义行为很模糊定义

的C FAQ定义这样的未定义行为:


  

任何事情都有可能发生;标准并没有规定要求。该
  程序可能无法编译,或者它可能不正确地执行(或者
  崩溃或静默产生不正确的结果),或者它可
  偶然做程序员意图到底是什么。


它主要是指任何事情都有可能发生。当你做这样的:

 的char * str中;
  scanf函数(%S,STR);

它的一个UB。有时你会得到你不应该成果,你认为它working.That的调试器哪里进来的几乎每一次handy.Use他们,尤其是在开始。其他推荐w.r.t程序:


  • 代替scanf函数的()使用与fgets()阅读字符串。如果你想用scanf函数然后使用它像 scanf函数(%WS,名); 其中name是字符数组和是W 是字段宽度。

  • 编译使用的 -Wall 选项让所有的警告,如果你要使用它,你可能已经得到了您正在使用STR未初始化的警告

在阅读这篇文章去,它有足够的信息来清楚你的疑惑。

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");

解决方案

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 w.r.t 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.

这篇关于scanf()函数不读取输入字符串当空字符串的早期定义的数组的第一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆