1

I have defined 2 structs:

typedef struct Book
    {
        char* name;
        char ID[11];
    }Book;

and

   typedef struct Library
    {
        char name[MAX];
        int num_books;
        Book *books;
    }Library;

my question is in this function:

void input_book(Book* B, FILE* in)
{
    char s[MAX+1]; 
    if (fscanf(in, "%s %s", (B->ID), s) != 0); 
        B->name = (char*)malloc((strlen(s) * sizeof(char)) + 1);
    if (B->name == NULL)
        Get_Lost("Couldn't open file. Exiting..");
    else
    strcpy(B->name, s);
}

The part where I use fscanf, should there be an & before (B->ID)? It works both with & and without, I'd love an explanation as to why it should/shouldn't. I use Visual Studio 2019 and this code is in C language only

I'm quite new to coding so I hope I've provided enough information for you to work with, thanks!

Vamirion
  • 25
  • 3
  • The behaviour is similar to scanf(), as ID is an array, then ID is already an address by default and therefore you don't need to use &, recall that ID = &ID[0]. – CRM Apr 12 '20 at 10:00
  • https://stackoverflow.com/questions/5851177/operator-definition-for-arrays-in-c – Eraklon Apr 12 '20 at 10:04

0 Answers0