我只是想将字符串扫描到数组中.我究竟做错了什么? [英] I'm just trying to scan strings into an array. What am I doing wrong?

查看:37
本文介绍了我只是想将字符串扫描到数组中.我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <string.h>

int main(void) {

   const int NUM_VALS = 20;
   int i;
   int actualInput;
   char userString[actualInput][NUM_VALS];
   int matchCount = 0;

   scanf("%d", &actualInput);

   for (i = 0; i < actualInput; ++i) {
      scanf("%s", userString[i]);
      printf("%s", userString[i]);
   }



   return 0;
}

输出:

b'hellohi \ x80 \ x07 @ \ xd2 \ x05 @ \ x9a \ x16 [\ xea \ xccp \ xa6 \ x15 \ xf6 \ x18 + \ xbf \ x87 \ x8a#\ x14)\ x05 @ \ xfe \ x7f'b'\ x92 \ x1fk \ xb3 \ xfe \ x7f \ xfe \ x7f \ x118 \ x08 \ xe8 \ x03 \ x0eY \ x03k \ xb3 \ xfe \ x7f \ xfe \ x7f \ xb2Y {\ xe8C} 8 \ r \ x8b-u {\ x8cx86_64'F-8sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin/usr/sbin:/usr/bin:/sbin:/binsbin:/binTF-88tf8RELOAD =/usr/lib/x86_64-linux-gnu/coreutils/libstdbuf.so64-linux-gnu/coreutils/libstdbuf.sols/libstdbuf.soout

b'hellohi\x80\x07@\xd2\x05@\x9a\x16[\xea\xccp\xa6\x15\xf6\x18+\xbf\x87\x8a#\x14)\x05@\xfe\x7f'b'\x92\x1fk\xb3\xfe\x7f\xfe\x7f\x118\x08\xe8\x03\x0eY\x03k\xb3\xfe\x7f\xfe\x7f\xb2Y{\xe8C}8\r\x8b-u{\x8cx86_64'F-8sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin/usr/sbin:/usr/bin:/sbin:/binsbin:/binTF-88tf8RELOAD=/usr/lib/x86_64-linux-gnu/coreutils/libstdbuf.so64-linux-gnu/coreutils/libstdbuf.sols/libstdbuf.soout

我尝试了一些变体,在scanf函数中将userString [i]替换为userString.结果是输出我的最后一个字符串的50,000个输入.我不明白发生了什么.

I've tried some variations replacing userString[i] with userString in the scanf function. The result is outputting 50,000 inputs of my last string. I don't understand what's happening.

推荐答案

问题是此代码序列:

int actualInput;
char userString[actualInput][NUM_VALS];
int matchCount = 0; 

scanf("%d", &actualInput);

第一行声明一个名为 actualInput 的变量,但不为该变量赋值.

The first line declares a variable called actualInput but doesn't assign a value to that variable.

第二行使用中的值声明可变长度数组(avla) actualInput .使用未初始化的变量的值会导致未定义的行为,这基本上意味着在代码中这一点之后,任何事情都可能发生.根据您对问题的描述,可能发生的情况是 actualInput 为零或很小的数字,因此您得到的数组太小而无法容纳您的输入.

The second line declares a variable length array (VLA) using the value in actualInput. Using the value of an uninitialized variable results in undefined behavior, which basically means that after that point in the code, anything can happen. What's likely happening (based on your description of the problem) is that actualInput is either zero, or a small number, so you get an array that's too small to hold your input.

最后一行(带有 scanf )最后为 actualInput 分配一个值.您可能会认为,更改 actualInput 时,数组将自动调整大小.那绝对不会发生.在C语言中,创建VLA后,其大小无法更改.

The last line (with the scanf) finally assigns a value to actualInput. You may be thinking that the array will resize itself when actualInput is changed. That definitely does not happen. In C, after a VLA is created, its size cannot be changed.

解决方案很简单,重新排列代码,以使事情按正确的顺序完成:

The solution is simple, rearrange the code so that things are done in the proper order:

int actualInput;
scanf("%d", &actualInput);
char userString[actualInput][NUM_VALS];

int matchCount = 0;

请注意,在使用该数字创建数组之前,您应该真正进行一些错误检查,以确保用户输入了合理的数字.例如

As a side note, you should really do some error checking to make sure that the user inputs a reasonable number, before using that number to create an array. For example

int actualInput;
if (scanf("%d", &actualInput) != 1 || actualInput < 1 || actualInput > 1000)
{
    printf("That is not a valid array size\n");
    return 1;
}
char userString[actualInput][NUM_VALS];

这篇关于我只是想将字符串扫描到数组中.我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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