LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2005, 10:33 PM   #1
trutnev
Member
 
Registered: Sep 2003
Distribution: RedHat 9.0, Mandrake 9.1
Posts: 68

Rep: Reputation: 15
Function Pointer as an Argument in C


more particulary, i need it for qsort function. when I do it like this

qsort( table, size, sizeof( element), &compare);

compiler tells me

warning: passing arg 4 of `qsort' from incompatible pointer type

compare is defined as a normal function.

what is wrong here ?

thank you.
 
Old 05-23-2005, 11:03 PM   #2
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
The "&" character is not needed there.

See "man bsearch" for example code.
 
Old 05-23-2005, 11:17 PM   #3
trutnev
Member
 
Registered: Sep 2003
Distribution: RedHat 9.0, Mandrake 9.1
Posts: 68

Original Poster
Rep: Reputation: 15
there is no example in my man for bsearch nor for qsort. just generic informations about the usage.

could you post here your man with the example ?

thanks in advance.


Quote:
Originally posted by Harmaa Kettu
The "&" character is not needed there.

See "man bsearch" for example code.
 
Old 05-24-2005, 01:24 AM   #4
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
Online version here.
 
Old 05-24-2005, 01:47 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Make sure your function is a 'int(const void*, const void*)' otherwise it won't work: you can't use:
Code:
int compare(int A, int B)
{ return A < B; }
You would need:
Code:
int compare(const void *A, const void *B)
{ return *((const int*) A) < *((const int*) B) ;}
ta0kira
 
Old 05-24-2005, 10:22 AM   #6
trutnev
Member
 
Registered: Sep 2003
Distribution: RedHat 9.0, Mandrake 9.1
Posts: 68

Original Poster
Rep: Reputation: 15
thank you
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Argument 2 - Pointer Error ?? Easy Fix?? Mistro116@yahoo.com Programming 1 10-04-2005 07:04 AM
How to declare a defaul argument in my function? bigapple Programming 3 08-11-2005 10:27 PM
C++ Function Argument gamehack Programming 1 09-13-2004 08:50 AM
c question: function with varied input argument blackzone Programming 1 09-12-2004 11:20 PM
passing function pointer as argument worldmagic Programming 7 08-04-2004 03:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration