0
votes

I'm writing a program to input book information using typedef and function.

Here is my declaration using typedef:

typedef struct {
    char name[1000], author[1000], publisher[1000], description[1000], ISBN[15];
} book;

Here is the function used to add book info:

void addBook(int* n, book list[1000]) {
    printf("Enter number of book you want to add: ");
    fpurge(stdin);
    scanf("%d", n);
    int i;
    for (i = 0; i < *n; i++) {
        printf("Book title: ");
        fpurge(stdin);
        gets(list[i].name);
        printf("Book author: ");
        fpurge(stdin);
        gets(list[i].author);
        printf("Publisher: ");
        fpurge(stdin);
        gets(list[i].publisher);
        printf("Description: ");
        fpurge(stdin);
        gets(list[i].description);
        printf("ISBN: ");
        fpurge(stdin);
        gets(list[i].ISBN);
    }
}

And the final is main:

int main(int argc, char** argv) {
    int n, list[1000];
    addBook(&n, list);
    return (EXIT_SUCCESS);
}

So when I tried to run, it worked. However, the compiler showed me two warning:

  1. warning: passing argument 2 of 'addBook' from incompatible pointer type [-Wincompatible-pointer-types]

  2. expected 'book *' but argument is of type 'int'

My question is: Are these warnings considered as errors? If yes, what is the correct solution? If no, why the compiler showed it?

P/s: I use Netbean IDE.

2

2 Answers

1
votes

Are these warnings considered as errors? I

In C most every warning if ignored leads to more or less fatal behaviour sooner or later. In may such cases the infamous Undefined Behaviour is invoked, which means that from that moment on anything can happen. The code might work, or not, sometimes only, or just Mondays or ...

So take these warnings serious and fix your code accordingly.

To do so change

 int n, list[1000];

to be

 int n;
 book list[1000];     
0
votes

Are these warnings considered as errors? If yes, what is the correct solution? If no, why the compiler showed it?

You should treat warnings as errors, really. Why? The compiler today might issue a warning for some unsafe operation, but the next version of the compiler might treat it as an error.

A warning would also typically denote you're performing some unsafe or undefined operation - the fact it works just means you got lucky. So: treat warnings as errors.

I suspect the reason it worked was because you happened to allocate a pointer to some memory and you just got lucky that it worked.