尝试通过struct

时间:2019-04-26 21:24:06

标签: c string pointers struct heap

我的程序应该通过fgets从用户那里得到一个字符串,并放入指向结构体的指针内的动态字符串数组(char **)中,然后进行打印。 相反,每次打印时都会出现错误。

在该示例中,我只会打印列表的第一部分,因为它始终会发送错误

这是我的结构:

typedef struct list
{
    char** items;
    int count; //number of items in the list.
}list;

我将“列表”发送给从用户获取输入的函数之前的代码:

list tempList; // Generic names to demonstrate the case
list *myList = &tempList;

// Resetting the list to default values...
myList->count = 0;
myList->items = (char**)malloc(1); 
//Setting the string array size to 1, later i increase it as i get input from the user

myList = addItem(myList);
list *addItem(list *myList)
{
    /*
    The function gets a list adds a string from the user and raises the count by 1
    */
    char tempStr[STR_LEN] = ""; //temp string so i can later assign it dynamically 
    int size = sizeof(myList->items); //getting the existing length of the list
    // getting the string
    printf("Enter String:\n"); 
    fgets(tempStr, STR_LEN, stdin);
    //

    myList->items = realloc(myList->items, size + 1); //adds room for 1 more item in the list
    size = size + 1;
    myList->items[size - 1] = malloc(strlen(tempStr)); //accesing the cell and assigning memory 
    strcpy(myList->items[size - 1], tempStr);
    myList->count++;
    return myList;
}

最后,打印项目:

printf("%s", myList->items[0]);

我知道这是很多代码,但这只是我所能展示的

在应该打印时从用户那里得到输入后,VS暂停程序并在新窗口中打开stdio.h,显示“抛出异常:读取访问冲突”。

我以前从未犯过这个错误,我一生都无法弄清这是什么意思。

非常感谢您提供各种帮助

1 个答案:

答案 0 :(得分:0)

您的错误是:

 int size = sizeof(myList->items);

不提供先前分配的动态数组的大小

使用 count 知道已经存在的元素数量

 myList->items = realloc(myList->items, size + 1); //adds room for 1 more item in the list

重新分配的内存不足,因为大小不是元素的数量(没有 char ),必须为:

myList->items = realloc(myList->items, (myList->count + 1) * sizeof(char *)); //adds room for 1 more item in the list

,并且您不需要变量 size ,因此( malloc 必须是字符串的长度,且空字符必须大于1):

myList->items[myList->count] = malloc(strlen(tempStr) + 1); //accesing the cell and assigning memory 
strcpy(myList->items[myList->count], tempStr);
myList->count++;

使用

初始化时请注意
 myList->items = (char**)malloc(1);

你可以做

myList->items = malloc(0);

cast 没用


addItem 返回列表毫无用处,因为它始终是参数 myList


获得所有评论的代码(我也从一开始删除了无用的 myList

list tempList; // Generic names to demonstrate the case

// Resetting the list to default values...
tempList.count = 0;
tempList.items = malloc(0); 
//Setting the string array size to 0, later i increase it as i get input from the user

addItem(&tempList);

void addItem(list *myList)
{
    /*
    The function gets a list adds a string from the user and raises the count by 1
    */
    char tempStr[STR_LEN]; //temp string so i can later assign it dynamically 

    // getting the string
    printf("Enter String:\n"); 
    fgets(tempStr, STR_LEN, stdin);
    //

    myList->items = realloc(myList->items, (myList->count + 1) * sizeof(char *)); //adds room for 1 more item in the list

    myList->items[myList->count] = malloc(strlen(tempStr) + 1); //accesing the cell and assigning memory 
    strcpy(myList->items[myList->count], tempStr);
    myList->count++;
}

请注意,您不要检查 fgets 的返回值(对于EOF情况),也不要在末尾删除可能的换行符