【问题标题】:How do I insert char* to array of structures?如何将 char* 插入结构数组?
【发布时间】:2020-04-14 20:12:54
【问题描述】:

任务是为结构数组动态分配内存,然后从键盘填充它们。我能够为数组中的每个结构实例动态分配和填充页面数量,但是当我尝试通过执行以下操作将 char* 添加到其中时: strcpy(myArray[i]->author, authorName);

但是每次我得到分段错误,那我做错了什么? 有没有可能这个问题实际上出在内存分配上?

这里是代码

#include <stdlib.h>
#include <string.h>

struct Book {
    char* author;
    char* title;
    int pages;
    int pubYear;
    int copies;
};

void allocList(struct Book **myArray, int booksAmount);
void fillKeyboard(struct Book **myArray, int booksAmount);

int main(void) {

    struct Book *booksList = NULL;

    int booksAmount = 3;

    allocList(&booksList, booksAmount);

    fillKeyboard(&booksList, booksAmount);

  return 0;
}

void allocList(struct Book **myArray, int booksAmount) {
    *myArray = (struct Book*) malloc(sizeof(struct Book) * 100);
    printf("memory for %d books was allocated \n", booksAmount);
}

void fillKeyboard(struct Book **myArray, int booksAmount) {

    int i = 0;

    char* authorName = "author name";

    while (booksAmount--) {
        printf("book number %d \n", i + 1);
        printf("enter amount of pages: ");
        scanf("%d", &(*myArray)[i].pages);
        printf("\nenter author: ");
        strcpy(myArray[i]->author, authorName);
        printf("%s is \n", authorName);
        i++;
        printf("\n");
    }

}

谢谢。

【问题讨论】:

  • cc++ 是具有不同概念的不同语言,答案会有所不同。因此,请删除其中一个标签以明确您使用哪种语言。根据您提出的关于 c 而不是 c++ 的问题的代码
  • strcpy(myArray[i]-&gt;author, authorName); 最有可能是这里的罪魁祸首。
  • 请注意,代码永远不会要求新的authorName
  • 你必须为author分配内存调用之前strcpy。即:(*myArray)[i].author = malloc(MAX_AUTHOR_NAME_LENGTH+1);。您的 strcpy 调用的参数也不正确。它必须是strcpy((*myArray)[i].author, authorName);。您在函数fillKeyboard 中对参数myArray 的额外间接使代码不必要地复杂化。

标签: c struct dynamic-memory-allocation


【解决方案1】:

如果您关心内存,则不应创建包含 100 本书的数组。避免此类分配的一个好方法是链表,因此您的程序将始终只分配所需的内存。您的 Book 结构将如下所示:

struct Book {
    char* author;
    char* title;
    int pages;
    int pubYear;
    int copies;
    struct Book *next;
};

程序肯定会稍微复杂一些,但结果是干净的内存使用和真正动态的程序。

【讨论】:

    【解决方案2】:

    myArray[i].author 是一个字符串。 所以(在C中)一个char数组。 作为一个数组,你需要用 malloc 分配它

     myArray[i].author=malloc(sizeof(char) * 100);
    

    您的 while 循环应如下所示:

     while (booksAmount--) {
        myArray[i].author=malloc(sizeof(char) * 100);
        printf("book number %d \n", i + 1);
        printf("enter amount of pages: ");
        scanf("%d", &(myArray)[i].pages);
        printf("\nenter author: ");
        strcpy(myArray[i].author, authorName);
        printf("%s is \n", authorName);
        i++;
        printf("\n");
    }
    

    请记住,100 是一个“神奇的数字”,所以如果作者姓名超过 100 个字符,它将不起作用

    编辑:与标题相同,您需要在数组元素中分配所需的内存

    【讨论】:

    • 关闭 1,“如果作者姓名长于 99”。如果 authorNam 在分配前已知,IAC 会考虑 malloc(strlen(authorNam)+1);
    • 该代码不正确。表达式myArray[i].author 没有意义。必须改为(*myArray)[i].author。原始海报对参数myArray 的额外间接使代码变得不必要地复杂。
    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    相关资源
    最近更新 更多