[英]I'm just trying to scan strings into an array. What am I doing wrong?
#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;
}
Output:输出:
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
I've tried some variations replacing userString[i] with userString in the scanf function.我尝试了一些变体,在 scanf 函数中用 userString 替换 userString[i]。 The result is outputting 50,000 inputs of my last string.
结果是输出了我最后一个字符串的 50,000 个输入。 I don't understand what's happening.
我不明白发生了什么。
The problem is this sequence of code:问题是这个代码序列:
int actualInput;
char userString[actualInput][NUM_VALS];
int matchCount = 0;
scanf("%d", &actualInput);
The first line declares a variable called actualInput
but doesn't assign a value to that variable.第一行声明了一个名为
actualInput
的变量,但没有为该变量赋值。
The second line declares a variable length array (VLA) using the value in actualInput
.第二行使用
actualInput
的值声明了一个可变长度数组 (VLA) 。 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.可能发生的情况(根据您对问题的描述)是
actualInput
要么为零,要么是一个很小的数字,因此您得到的数组太小而无法容纳您的输入。
The last line (with the scanf
) finally assigns a value to actualInput
.最后一行(带有
scanf
)最终为actualInput
分配了一个值。 You may be thinking that the array will resize itself when actualInput
is changed.您可能认为当
actualInput
更改时,数组会自行调整大小。 That definitely does not happen.那绝对不会发生。 In C, after a VLA is created, its size cannot be changed.
在 C 中,创建 VLA 后,其大小不能更改。
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];
you cant declare it as a 2D array then treat it as a normal array .您不能将其声明为二维数组,然后将其视为普通数组。 each case should include only one letter but it can't be done automatically , I suggest you add this :
每个案例应该只包含一个字母,但不能自动完成,我建议您添加以下内容:
for (i = 0; i < actualInput; ++i)
{
gets(stri);
for (k=0;k<strlen(stri);k++)
userString[i][j]=stri[j];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.