pointer problem

K

kc

Hi,
i am getting a warning when compiling my program...
blbp_eom_isr_revenue.c", line 739: warning 527: Integral value
implicitly converted to pointer in assignment.
blbp_eom_isr_revenue.c", line 739: warning 563: Argument #4 is not the
correct type.

and at line 739:-

qsort((void *)Buff, totalRecs, sizeof(strcture_name), (int)
(*blbp_CtnRecCompare));

and the function blbp_CtnRecCompare is -
int blbp_CtnRecCompare (strcture_name Rec_1,
strcture_name Rec_2)
{
return (strcmp (Rec_1.field_name, Rec_2.field_name));
}

Thanks for the solutions.
 
W

Walter Roberson

i am getting a warning when compiling my program...
blbp_eom_isr_revenue.c", line 739: warning 527: Integral value
implicitly converted to pointer in assignment.
blbp_eom_isr_revenue.c", line 739: warning 563: Argument #4 is not the
correct type.
and at line 739:-
qsort((void *)Buff, totalRecs, sizeof(strcture_name), (int)
(*blbp_CtnRecCompare));
and the function blbp_CtnRecCompare is -
int blbp_CtnRecCompare (strcture_name Rec_1,
strcture_name Rec_2)

blbp_CtnRecCompare is a function, but in most cases when you aren't
actually calling the function, mentioning the name of the function
silently gets you the address of the function instead.

In your call to qsort, you then take that function address and
try to dereference it, which gets you back to the function again.
Your (int) in front of that then converts that from whatever type
it is (probably a function pointer) to an integral type, probably
losing address information along the way. qsort() expects a function
pointer at that point, so C is going to try to convert the int
into a function pointer, which is what is generating the first warning
about integral value converted to pointer. And because the integral
value is not the type of function pointer that qsort expects, you
get the second warning.

In order to fix the problem, you need to cast blbp_CtnRecCompare
to the correct type as you pass it in. Your reference book should
have several examples of the correct syntax.
 
B

Ben Pfaff

kc said:
qsort((void *)Buff, totalRecs, sizeof(strcture_name), (int)
(*blbp_CtnRecCompare));

This attempts to cast a pointer to function to type `int'.

Also, the cast to void * is suspicious.

kc said:
int blbp_CtnRecCompare (strcture_name Rec_1,
strcture_name Rec_2)
{
return (strcmp (Rec_1.field_name, Rec_2.field_name));
}

This is not the correct type. It should be declared as
int blbp_CtnRecCompare (const void *, const void *);

If you declare it correctly, you don't need any cast at all in
the call to qsort.
 
B

Ben Pfaff

(e-mail address removed)-cnrc.gc.ca (Walter Roberson) writes:

[call to qsort]
In order to fix the problem, you need to cast blbp_CtnRecCompare
to the correct type as you pass it in.

No. The OP needs to declare blbp_CtnRecCompare to be of the
correct type. Then the cast is unnecessary.

If the cast is necessary, then the call to qsort is wrong.
 
W

Walter Roberson

(e-mail address removed)-cnrc.gc.ca (Walter Roberson) writes:
[call to qsort]
In order to fix the problem, you need to cast blbp_CtnRecCompare
to the correct type as you pass it in.
No. The OP needs to declare blbp_CtnRecCompare to be of the
correct type. Then the cast is unnecessary.
If the cast is necessary, then the call to qsort is wrong.

K&R2 section 5.11 "Pointers to Functions", page 119

[...]
void qsort(void *lineptr[], int left, int right,
int (*comp)(void *, void *));
int numcmp(char *, char *);
[...]
qsort((void **) lineptr, 0, nlines-1,
(int (*)(void*,void*))(numeric ? numcmp : strcmp));
[...]



Did something relevant change between the publication of K&R2 and
the C89 standard such that K&R2 became incorrect ?
 
B

Ben Pfaff

(e-mail address removed)-cnrc.gc.ca (Walter Roberson) writes:
[call to qsort]
In order to fix the problem, you need to cast blbp_CtnRecCompare
to the correct type as you pass it in.
No. The OP needs to declare blbp_CtnRecCompare to be of the
correct type. Then the cast is unnecessary.
If the cast is necessary, then the call to qsort is wrong.

K&R2 section 5.11 "Pointers to Functions", page 119

[...]
void qsort(void *lineptr[], int left, int right,
int (*comp)(void *, void *));
int numcmp(char *, char *);
[...]
qsort((void **) lineptr, 0, nlines-1,
(int (*)(void*,void*))(numeric ? numcmp : strcmp));
[...]



Did something relevant change between the publication of K&R2 and
the C89 standard such that K&R2 became incorrect ?

This part of K&R2 is badly written, as admitted on the official
errata page at:
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html

The relevant text is:

119-121(5.11): The qsort discussion needs recasting in
several ways. First, qsort is a standard routine in ANSI/ISO
C, so the rendition here should be given a different name,
especially because the arguments to standard qsort are a bit
different: the standard accepts a base pointer and a count,
while this example uses a base pointer and two offsets.

Also, the comparison-routine argument is not treated
well. The call shown on p 119, with an argument

(int (*)(void*,void*))(numeric? numcmp : strcmp)

is not only complicated, but only barely passes muster. Both
numcmp and strcmp take char * arguments, but this expression
casts pointers to these functions to a function pointer that
takes void * arguments. The standard does say that void * and
char * have the same representation, so the example will
almost certainly work in practice, and is at least defensible
under the standard. There are too many lessons in these
pages.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top