尝试通过struct从动态字符串数组中打印字符串时出错

问题描述 投票:0回答:1

我的程序应该从用户通过fgets获取一个字符串到一个指向结构的指针内的动态字符串数组(char **),然后打印它。相反,每当我打印时我都会收到错误。

在示例中,我将仅打印列表的第一部分,因为它无论如何都会发送错误

这是我的结构:

typedef struct list
{
    char** items;
    int count; //number of items in the list.
}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,说“抛出异常:读取访问冲突”。

我以前从来没有得过这个错误,我不能为我的生活找出它意味着什么。

感谢先进的任何帮助

c string pointers struct heap
1个回答
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

而且你不需要变量大小,所以(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);

演员没用


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情况),也不删除最后可能的换行符

© www.soinside.com 2019 - 2024. All rights reserved.