0

Im new to programming and im having a hard time learning DMA and trying to work with structs and pointer at the same time. im working on a program that takes in information about books and stores the author and title in an array of structs to be displayed it requires DMA to store the strings in the struct.

my hardest part to understand and trying to fix is when i try to access fields of an Struct array in a function definition for example:

void getInfo(struct BookInfo *pbook, char author[], char title[])
{
     //creating memory for strings 
     pbook.author = (struct BookInfo*) malloc((strlen(author) +1) * sizeof(char));       
     pbook.title = (struct BookInfo*) malloc((strlen(title) +1) * sizeof(char)); 

     //copying info into the heap
     strcopy(pbook.author, author);
     strcopy(pbook.title, title);

}


I would really appreciate your help in any way, thanks in advance

This is my full code:

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

#define size 5   //size is the total number of elements in the array

//declaration of struct
struct BookInfo {

    char* author;
    char* title;
};

//prototypes
void getInfo(struct BookInfo *pbook, char author[], char title[]);
void printInfi(struct BookInfo book[]);

int main() {

    struct BookInfo myBook[size];    //declare an array.
    char author[40] = { '0' };   //temp strings to store input from user
    char title[50] = { '0' };


    //get input from user
    printf("Enter author name: ");
        fgets(author, 40, stdin);

    printf("Enter book title name: ");
        fgets(title, 50, stdin);


   // call function to store info (dma) individually in array, loop 5 times do fill struct array 
     for(int i =0; i < size; i++)
         {
             void getInfo(struct BookInfo myBook, author, title);
         }

   // call function to print all info from array, call one time
      void printInfi(struct BookInfo myBook);

   // Free space from dma
       for(int i=0; i < size; i++)
           {
              free(myBook[i].author);
              free(myBook[i].title); 
           }

    return 0;
}


void getInfo(struct BookInfo *pbook, char author[], char title[])
{
     //creating memory for strings 
     pbook.author = (struct BookInfo*) malloc((strlen(author) +1) * sizeof(char));       
     pbook.title = (struct BookInfo*) malloc((strlen(title) +1) * sizeof(char)); 

     //copying info into the heap
     strcopy(pbook.author, author);
     strcopy(pbook.title, title);

}

void printInfo(struct BookInfo book[])
{
     for(int i = 0; i < size; i++)   
         {
             printf("Title: %s, Author: %s\n", book[i].author, book[i].title);
         }

}

JeanBook
  • 21
  • 4
  • Is your question why `pbook.author` does not compile? – M Oehm May 20 '20 at 15:11
  • `pBook` is a pointer. You need the `->` operator to access its fields, not the `.` operator. Like this: `pbook->author = ...`. – Oppen May 20 '20 at 15:13
  • I tried using the arrow operator but it wont let me access the field of the struct in the function, im trying to call getInfo in main, and fill the array struct, but in the function since i provide the array, which would be the first address, idk how to fill the other struct fields like pbook[0].author = the pointer from malloc – JeanBook May 20 '20 at 16:09

1 Answers1

0

If you have a structure pointer example: struct structName *pToAStruct;, to access the value of a field use -> operator like this : var = pToAStruct->field. With var and field having the same type, int for example.

If you have a directly the structure variable, then use . operator. Example: struct structName AStruct; var = AStruct.field;

Beware in these examples I assumed you have allocated memory / initialized the structure when its needs to be.

Welgriv
  • 492
  • 5
  • 14