简体   繁体   English

C编程:gets()和Char String Array问题 - strcpy()复制到Array中的其他字符串

[英]C Programming : gets() and Char String Array Issue - strcpy() copying into other strings in the Array

I am using the function gets() to retrieve string input from the user. 我正在使用函数gets()来检索用户的字符串输入。 I then store that string into a char array, char transdestmp[DESMAX], where DESMAX is 31. If the variable_name is greater than 30 chars, then ask the user to renter another string. 然后我将该字符串存储到char数组,char transdestmp [DESMAX],其中DESMAX为31.如果variable_name大于30个字符,则要求用户租用另一个字符串。 Else, copy the string using strcpy(), into a 2 dimensional array char - acctdes[31][20]. 否则,使用strcpy()将字符串复制到二维数组char - acctdes [31] [20]。

accttitle recieves transdestmp[DESMAX] accttitle收到transdestmp [DESMAX]

void accttitle(char descr[DESMAX])
{
    printf("\nEnter title for new account %d: ", transinpt);
    gets(descr);
    while(strlen(descr)>DESMAX){
        printf(" **Title entered is longer than 30 characters\n");
        printf(" Please reenter : ");
        gets(descr);
    }
    strcpy(acctdes[transcntr],descr);
    printf("---->vacctdes[transcntr]: %s\n", acctdes[transcntr]);
    printf("---->vacctdes[transcntr-1]: %s\n", acctdes[transcntr-1]);
}

For some reason when I input a long string, and then enter another string, apart of the second string acctdes[1] overwrites the other string stored in acctdes[0]. 出于某种原因,当我输入一个长字符串,然后输入另一个字符串时,第二个字符串的一部分acctdes [1]会覆盖存储在acctdes [0]中的另一个字符串。

for example, 例如,

First input: acctdes[0] = "This is a long string" 第一个输入:acctdes [0] =“这是一个长字符串”

It works... 有用...

Second input acctdes[1] = "monkey" 第二个输入acctdes [1] =“猴子”

It works... 有用...

but then, it seems that when I output acctdes[0], acctdes[0] has some of the value from acctdes[1]... like output - This is a long monk... 但是,似乎当我输出acctdes [0]时,acctdes [0]有一些来自acctdes [1]的值......就像输出一样 - 这是一个长僧......

Please let me know if you would like more information. 如果您想了解更多信息,请与我们联系。 Thanks in advance. 提前致谢。

Your array declaration should be other way round. 您的数组声明应该是相反的。

Currently you have : acctdes[31][20] , which means 31 place holders for 20 char length each; 目前你有: acctdes[31][20] ,这意味着31占位符,每个20字符长度; while you want 20 placeholders for 31 char length each. 而你想要20个占位符,每个31个字符长度。

It should be changed to acctdes[20][31] 它应该改为acctdes[20][31]

"I am using the function gets() to retrieve string input from the user." “我正在使用函数gets()来检索用户的字符串输入。”

That's your problem, or at least part of it. 这是你的问题,或至少是其中的一部分。

Never use the gets() function. 永远不要使用gets()函数。 It is inherently unsafe unless you have complete control over what input will appear on stdin . 除非您完全控制将在stdin显示的输入,否则它本质上是不安全的。 It has no mechanism to specify how many characters of input will be accepted. 它没有机制来指定接受多少个输入字符。 If the user enters more data than will fit in the target array, your program's behavior is undefined. 如果用户输入的数据多于目标数组中的数据,则程序的行为未定义。

Use fgets() instead; 请改用fgets() ; it takes an argument specifying the size of the target buffer. 它需要一个参数来指定目标缓冲区的大小。 You'll still have to deal with the possibility that the input line is too long (in that case, fgets() just stores a partial line). 您仍然需要处理输入行太长的可能性(在这种情况下, fgets()只存储部分行)。 If the input line isn't too long, fgets() leaves the '\\n' in the buffer, unlike gets() . 如果输入行不是太长, fgets()会在缓冲区中留下'\\n' ,与gets()不同。

It's so bad that it's been removed from the latest (2011) ISO C standard. 它已经从最新的(2011)ISO C标准中删除了。

(See also the other answers.) (另见其他答案。)

You have the dimensions on your array reversed. 您的阵列上的尺寸已反转。 Try acctdes[20][31] . 尝试acctdes[20][31]

The reason the strings bleed into each other is because C lays out a two-dimensional array as one long block of memory. 字符串相互渗透的原因是因为C将二维数组布置为一个长存储器块。 When you do acctdes[2] its really doing pointer arithmetic under the hood like *(acctdes + (31 * 2)) to skip over the first part of the memory block to get to your third element. 当你做acctdes[2]它真的在引擎盖下像*(acctdes + (31 * 2))跳过内存块的第一部分来到你的第三个元素。 So if one string writes past its bounds, it will end up in the next string. 因此,如果一个字符串写入超过其边界,它将最终在下一个字符串中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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