简体   繁体   English

数组填充本身只有最后一个用户输入的输入字符串

[英]Array filling itself with only the last user entered inputed string

I m trying to make a piece of code that prompts the user for a number of string, n, dynamically allocates an array of n strings, and then prompts the user to enter n strings. 我试图创建一段代码,提示用户输入一些字符串,n,动态分配n个字符串的数组,然后提示用户输入n个字符串。

The problem I am having is the array is only showing itself being filled with the last string the user entered. 我遇到的问题是数组只显示自己被用户输入的最后一个字符串填充。

Example: Program prompts user for n User enters 3. User enters "test1" for the first element in the array User enters "test2" for the second element in the array User enters "test3" for the third element in the array 示例:程序提示用户输入n用户输入3.用户输入“test1”表示数组中的第一个元素用户输入“test2”表示数组中的第二个元素用户输入“test3”表示数组中的第三个元素

I go to print the contents of the array, and it says each element is "test3" 我去打印数组的内容,它说每个元素都是“test3”

Here is the code: (flush_buffer() and strip_newline() are functions I wrote that are unimportant to the problem I am having) 这是代码:(flush_buffer()和strip_newline()是我写的函数,对我遇到的问题不重要)

printf("How many strings?\n");
scanf("%d", &max_strings);
flush_buffer();

string_array = (char**) malloc(max_strings * sizeof(char*));

for(i = 0; i < max_strings; i++)
{
    scanf("%s", temp);

    strip_newline(temp);

    string_array[i] = temp;

    printf("string_array[%d] is: %s\n", i, string_array[i]);
}
    for(i = 0; i < max_strings; i++)
{
    printf("i: %d\n", i);
    printf("string_array[%d] is: %s\n", i, string_array[i]);
}

Any ideas on what I am missing here? 关于我在这里缺少什么的想法?

With the assignment 随着任务

string_array[i] = temp;

you make all pointers in string_array point to the same place . 你使string_array所有指针指向同一个地方


I suggest you use strdup to duplicate the string instead: 我建议你使用strdup来复制字符串:

string_array[i] = strdup(temp);

Of course, this means you have to free all strings in the collection. 当然,这意味着您必须free集合中的所有字符串。

When you assign the strings: 分配字符串时:

string_array[i] = temp;

you just store a pointer of the temporary buffer in each string, which will be overwritten after the next string is read. 您只需在每个字符串中存储临时缓冲区的指针,在读取下一个字符串后将覆盖该指针。 In other words, all of your strings have the same value, namely temp , which has the contents of the last string read. 换句话说,所有字符串都具有相同的值,即temp ,它具有读取的最后一个字符串的内容。

If you want to store your strings, you must allocate memory for string_array[i] and then copy the contents with strcpy . 如果要存储字符串,则必须为string_array[i]分配内存,然后使用strcpy复制内容。 Alternatively, you can use a shortcut: 或者,您可以使用快捷方式:

string_array[i] = strdup(temp);

Note that strdup allocates memory internally that you must free() at some point. 请注意, strdup内部分配内存,您必须在某个时刻free()

you can use this code: 你可以使用这段代码:

int max_strings;
printf("How many strings?\n");
scanf("%d", &max_strings);

//allocate memory
char **string_array = (char**) malloc(max_strings * sizeof(char*));
for (int i = 0; i < max_strings; i++)
{
    string_array[i] = (char*)malloc(sizeof(char) * 50);
}

for(int i = 0; i < max_strings; i++)
{
    scanf("%s", string_array[i]);

    printf("string_array[%d] is: %s\n", i, string_array[i]);
}
for(int i = 0; i < max_strings; i++)
{
    printf("i: %d\n", i);
    printf("string_array[%d] is: %s\n", i, string_array[i]);
}

//free memory
for (int i = 0; i < max_strings; i++)
{
    free(string_array[i]);
}
free(string_array);

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

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