"Warning comparison between Pointer and integer" - what is it?

Asked

Viewed 3,094 times

0

during the compilation of the code in C language I appeared this error:

[Warning] comparison between Pointer and integer

for this step of the code:

    if(livro > TAM) {
        printf("------- Você já cadastrou o maximo de livros ! -------");
    }

Being livro one struct:

struct fichaLivro {
    int cod;
    char obra[50];
    char autor[50];
    char editora[50];
}livro[TAM];

and TAM a constant:

#define TAM 5

What is happening and how can I solve the same?

  • I don’t understand the logic. You have a loop where the index varies from 0 to the maximum possible to be placed in the array, in the case of TAM-1, so it is obvious that at the end of the loop you have registered as many books, your test is meaningless.

  • The test is to indicate if the maximum number of books has been registered, how do I set the value of the maximum array of 5 will always be registered the maximum, but how will the user know if it has been registered if he tries to register more books? Initially I thought to compare the direct array with its size, but as Maniero reported in C the typing is static, in a layman’s way not to compare 'home' with 'boat', then I compared one of the elements of the array that will always be 5 items with the constant to inform if you already have as many books registered.

  • Let me try to explain. At the end of the loop the variable i, which is the variable that should be being tested in if (if (i > TAM)), will always be with the TAM value, since at no point in the loop you interrupt the loop, it will always go to the end (the i<TAM condition of the for command). In this way your test is innocuous because you will always have been registered as many books as possible and your variable i will never be greater than TAM, will be equal.

  • I understood, but how would I show that it is filled? (sorry my ignorance I am not a programmer in C)

  • Dude, you didn’t implement the option to partially fill just can fully fill in!

  • Now I get it, thanks for the help.

Show 1 more comment

1 answer

4

The error is exactly what is written, as you declared the variable livro as a array, it becomes a pointer to a sequence of data, already TAM is a numerical constant.

C is a static typing language, but weak, so it even lets you make comparisons even if it doesn’t make sense, on a pointer and a simple number are two completely different things it’s like you want to know if an apple and a horse are the same thing, You can’t even begin to compare, so most compilers warn that this will produce a meaningless result. The solution is to compare things that make sense, but we don’t even know exactly what would make sense there, what you’re trying to compare, the only sure thing is that a book can be compared to a 5.

Looking at the whole code on another question you might want to compare the id of the book, something like that:

if (livro.id > TAM) {

But then book would have to have only one book and not one array of books (what alias the variable should call livros and not livro.

If you’re really going to work with array, maybe I’m in a loop and I want to do something like this:

if (livro[i].id > TAM) {

I put in the Github for future reference.

But if there’s a noose maybe I’d like to do if (i > TAM) {.

But it may be that all options are wrong because the concept is wrong and should not even do this.

After editing, you can see that the problem shouldn’t even exist. The code is already running to register the maximum size, nor allows you to register less than the maximum, nor more, so this if does not make sense, even if create a control, which is the third option I had given up, it will never enter this if, then just take it off.

I won’t even try to show you another way to do it better because I already know you’d rather do it your way, even though he’s no good.

  • I studied a little and got some progress in the code percudo, I will update the same for better understanding, I appreciate the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.