473,382 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

pointer problem

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.

Sep 8 '06 #1
5 5295
In article <11**********************@e3g2000cwe.googlegroups. com>,
kc <kc***********@gmail.comwrote:
>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.
--
Prototypes are supertypes of their clones. -- maplesoft
Sep 8 '06 #2
"kc" <kc***********@gmail.comwrites:
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" <kc***********@gmail.comwrites:
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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Sep 8 '06 #3
ro******@ibd.nrc-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.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin
Sep 8 '06 #4
Walter Roberson said:
In article <87************@benpfaff.org>,
Ben Pfaff <bl*@cs.stanford.eduwrote:
<snip>
>
>>If the cast is necessary, then the call to qsort is wrong.

K&R2 section 5.11 "Pointers to Functions", page 119
....gives a lousy illustration of how to use qsort. Ben is right.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 8 '06 #5
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <87************@benpfaff.org>,
Ben Pfaff <bl*@cs.stanford.eduwrote:
>>ro******@ibd.nrc-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.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Sep 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
7
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this...
51
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
2
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I...
9
by: junky_fellow | last post by:
Hi, To print the pointer using printf(), we convert it to (void *) . printf("%p",(void *)ptr); My question is how printf() determine which type of pointer is passed to it and prints its value...
6
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.