LinuxQuestions.org
Help answer threads with 0 replies.
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 07-30-2011, 03:34 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
A question about function pointer in C...


What does this do exacty?
Code:
int(*fp)(void) = (int(*)(void))buf
This declares a function pointer that takes a void argument and returns an int. However I don't understand what this assignment does exactly.

Last edited by trist007; 07-30-2011 at 03:49 PM.
 
Old 07-30-2011, 03:58 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by trist007 View Post
What does this do exacty?
Code:
int(*fp)(void) = (int(*)(void))buf
This declares a function pointer that takes a void argument and returns an int. However I don't understand what this assignment does exactly.
int(*)() is a type and int(*fp)() is the way you use it.
Code:
typedef int(*f_pointer)();
f_pointer fp = (f_pointer) buf;
The assignment takes the address of the function so that *fp is a reference (in C++, anyway) to the function, which can be evaluated with ().
Code:
int value = (*fp)(); //normal function-pointer evaluation

int(&fp2)() = *fp; //also allowed in C++
value = fp2();
Kevin Barry

Last edited by ta0kira; 07-30-2011 at 04:00 PM.
 
Old 07-30-2011, 04:33 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
Ah ok I see, 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
( C ) How do you declare a function pointer where a parameter is a function pointer? spursrule Programming 5 11-27-2007 07:56 PM
calling a function from a pointer spx2 Programming 3 05-25-2006 04:52 PM
c char* pointer function question true_atlantis Programming 9 04-14-2006 01:29 PM
(I know I am inept) what is a pointer function in C ? cigarstub Programming 3 09-27-2005 05:06 PM
question about function pointer compile error dragondad Programming 5 09-19-2005 11:42 AM

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

All times are GMT -5. The time now is 10:37 PM.

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