How do I insert char* to array of structures?

Serghey Hmeli

the task is to dynamically allocate memory for the array of structures and then fill them from keyboard. I was able to dynamically allocate and fill amount of pages for each of the struct instance in array, but when I try to add char* to it by doing something like: strcpy(myArray[i]->author, authorName);

But every time I get segmentation error, so what am I doing wrong? Is it possible that problem is actually in memory allocation?

Here is the code

#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");
    }

}

Thank you.

lory9894

myArray[i].author is a string. so (in C) an array of char. as an array you need to allocate it with a malloc

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

your while loop should look like this:

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

keep in mind that 100 is a "magic number" so if the author name is longer than 100 character it's not going to work

edit: same thing with the title, you need to allocate the needed memory in your array's element

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I return a char array of an unknown value?

How do I find a character followed by a character in a char array Java?

How do I create a type alias for an array of char with a fixed size?

How do I construct an array of ctype structures?

How do I receive a char array in a C function?

How do I create a file descriptor backed by a simple char array?

How do I read a C char array into a python bytearray with cython?

How do I tokenize a char array input into a char and a string?

How Do I access data in a Matlab Array of Structures in a mex file

How do I add String to a char array?

How do I insert array value into object?

How can I insert value in a char array?

How do I use an array variable inside an array of structures?

How do I compare a a string array with a char array

How do I return a pointer to something in the middle of a char array?

How do I convert an unsigned char array into a string in C?

How do I insert a multidimensional array into another array in a loop?

how do i insert a string into an array in c

how do I free this char** array?

How do I correctly work with char pointer to array in C++?

How do I insert an array inside an empty array in PHP?

How do I cast /copy from a char array so that I may store it in a char pointer array?

How do I print elements of structures contained in an array

How do I read a stringstream into a char *[40] / char ** array?

How do I pass an array of structures to a function?

How do I use tolower() with a char array?

How do I add an element to an array of structures in C?

How do I initialize an array of pointers to structures in C?

How do I filter an array of structures' properties with a keyword?