FILIaS
FILIaS

Reputation: 505

Same code... but warning!Any ideas?

I've a question for a warning message that i get. For this line,using qsort library function:

qsort(catalog, MAX ,sizeof catalog, struct_cmp_by_amount); 

I get this warning:

warning: passing argument 4 of ‘qsort’ makes pointer from integer without a cast

EDIT:

struct_cmp_by_amount is the following function on the program.(--->) catalog is a struct and MAX is defined as 100

BUT,for another program with the same code, with the same exactly struct_cmp_by_amount function, i dont get that warning for the 4th argument!

EDIT: I've also have to say that on both programs i havent used prototypes of functions! But for the 2nd program it works normally in contrast to the 1st one.

qsort(structs, structs_len, sizeof(struct st_ex), struct_cmp_by_amount);

EDIT:

st_ex is a struct

struct st_ex structs[]={./*elements*/..}

size_t structs_len = sizeof(structs) / sizeof(struct st_ex);

int struct_cmp_by_amount(const void *a, const void *b)
{
    struct catalogue *ia = (struct catalogue *)a;
    struct catalogue *ib = (struct catalogue *)b;
    return (int)(100.f*ia->amount - 100.f*ib->amount);  
}

I'm wandering about why that's happening. Have you any ideas?

Upvotes: 0

Views: 443

Answers (6)

David Thornley
David Thornley

Reputation: 57036

The problem appears to be that, in one case, the qsort() call can see the declaration of struct_cmp_by_amount(), and in the other it can't. Since you aren't using function prototypes, I'd guess that in one case the function declaration was before the qsort() call and in the other case afterwards.

Also, the first qsort() call listed could cause problems, since catalog is the pointer or array you're passing, and sizeof catalog is the size you're giving it. If catalog is an array here, you're saying that it's made of MAX things as big as the whole array, which means you'll be messing with far more memory than you've allocated, and potentially causing no end of bugs. If catalog is a pointer, you're saying that it's pointing to an array of items of pointer size. Neither seems plausible. Using sizeof *catalog or sizeof catalog[0] would make much more sense.

Also, using floating-point numbers to represent dollar amounts is prone to error; you're usually better off using an integer type to represent the number of cents. That's a different problem, though.

Upvotes: 1

ChrisBD
ChrisBD

Reputation: 9209

Is this line of code copied correctly?:

qsort(catalog, MAX ,sizeof catalog, struct_cmp_by_amount);

If so it should surely be:

qsort(catalog, MAX ,sizeof(*catalog), struct_cmp_by_amount);

Also your example of a working qsort line uses struct_cmp_by_price not struct_cmp_by_amount.

So as has been pointed out best check your declarations there and whether they are in scope. The facft that the compiler has cast struct_cmp_by_amount as integer suggests that it can't find a declaration for it.

Upvotes: 0

kennytm
kennytm

Reputation: 523184

The prototype of struct_cmp_by_amount must be declared as

int struct_cmp_by_amount (const void* a, const void* b) {
  ...
}

to avoid the warning, even if you know a and b are some T*.

Also, make sure the forward declaration exists before calling qsort.

Upvotes: 1

JeremyP
JeremyP

Reputation: 86651

I assume you mean the qsort from the standard C library. The chances are that you have forgotten to include stdlib.h which declares the qsort function.

EDIT: Sorry, didn't read the error message properly. In fact, the other answer by John R. Strohm is probably right - you haven't got a declaration for the function that's visible to the compiler.

Upvotes: 0

John R. Strohm
John R. Strohm

Reputation: 7667

I'm guessing, since I can't see your code.

qsort() takes the array to be sorted, the number of entries, the sizeof an entry, and a pointer to a comparison routine that takes two pointers to entries and compares them. (I don't remember the return type of the function, or the convention.)

It looks as though the compiler can't see a function declaration or function prototype for your comparision routine, struct_cmp_by_amount, is therefore presuming that it is an int (according to C rules), and is warning you that it needs a pointer (to a function) in that parameter position.

Upvotes: 5

Puppy
Puppy

Reputation: 146910

Without knowing the types of the operands, then we can't resolve your type error.

Upvotes: 0

Related Questions