简体   繁体   English

我只是想将字符串扫描到数组中。 我究竟做错了什么?

[英]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.

相关问题 尝试实现字符串数组时,我在做什么错? - What am I doing wrong when trying to implement an array of strings? 我写了一个 C 程序来计算数组的标准偏差。 我的代码给出了错误的答案。 我究竟做错了什么? - I wrote a C program to calculate the standard deviation of an array. My code is giving the wrong answer. What am I doing wrong? 我正在尝试利用缓冲区溢出,我做错了什么? - I'm trying to exploit a bufferoverflow, am I doing something wrong? 我在 C 中的这个数组做错了什么? - What am I doing wrong with this array in C? 我收到“Invalid Initializer”,我做错了什么? - I'm getting "Invalid Initializer", what am I doing wrong? 我不知道我对这个数组做错了什么 - I don't know what I'm doing wrong with this Array malloc 和结构数组的重新分配我做错了什么? - What am I doing wrong with malloc and realloc of array of struct? 我正在尝试将C代码转换为Mathematica代码,我在做什么错? - I am trying to convert C code to Mathematica code, what am i doing wrong? 无法弄清楚我在使用 arrays 时做错了什么——警告我是新手,正在学习 - Can't figure out what i'm doing wrong with arrays — warning i am new and learning C 我在这段代码中做错了什么? - C What am i doing wrong in this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM