当我在linux机器上运行这段代码时,我注意到当我尝试迭代和存储各种用户输入的字符串时,它会导致整个数组只包含一个值。有人能帮我吗?
当我希望实际值放在我分配的数组中时,它似乎只保存buf的内存地址(从而为每个条目生成相同的字符串(。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char *name;
} Task;
int main()
{
/*Allocate 5 sets of Task structs onto the heap.
which we access the starting address with our pointer*/
Task *my_list = malloc(sizeof(Task)*5);
/*user input*/
char buf[100]="