c - 尝试通过结构从动态字符串数组打印字符串时出错

标签 c string pointers struct heap-memory

我的程序应该通过 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,并显示“抛出异常:读取访问冲突”。

我以前从未遇到过此错误,而且我一生都无法弄清楚它的含义。

最佳答案

您的错误是:

 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);

而且 Actor 毫无用处

<小时/>

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

关于c - 尝试通过结构从动态字符串数组打印字符串时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55875105/

相关文章:

swift - 如何将模式中包含其他文本的文本个性化并使用正则表达式对其进行编辑

javascript - jQuery 获取 href 值特定部分的字符串

c - 指针更改值而不作为参数传递

c - 在 C 中随机用 2​​ 个整数填充数组

c - 如何为 PIC18 制作定时器?

c - 在链接列表中存储字符串时遇到问题

python - SWIG Python - 包装一个需要双指针指向结构的函数

c++ - 访问c++'s vector pointer'的元素

c - 访问函数中 main 中存在的数组

c - 为什么我必须在指针上使用 free 而不是普通声明?