From: Shriramana Sharma <samjnaa@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Function pointers to inline functions
Date: Wed, 04 Apr 2007 22:39:22 +0530 [thread overview]
Message-ID: <4613DBC2.8060708@gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 539 bytes --]
Hello.
As per my understanding, an inline function is replaced in place by the
compiler with the body of the function, so it does not have a separate
location in memory in contrast with a regular function. This being so,
how is it possible to extract a pointer to an inline function and
effectively use it?
See the attached two examples. They work and though it's a good thing
for my project that I can extract a pointer to an inline function I do
not understand how it is possible.
Please explain. Thank you.
Shriramana Sharma.
[-- Attachment #2: function-pointers-1.cpp --]
[-- Type: text/x-c++src, Size: 1315 bytes --]
#include "stdio.h"
inline void square ( int a ) { printf ( "The square of %d is %d\n", a, a * a ) ; }
inline void cube ( int a ) { printf ( "The cube of %d is %d\n", a, a * a * a ) ; }
inline void fourthpower ( int a ) { printf ( "The fourth power of %d is %d\n", a, a * a * a * a ) ; }
void ( * functionpointers [ 3 ] ) ( int ) ;
/*
the above line declares an array called functionpointers that has three ( 3 ) elements which are all pointers ( * ) to functions which take a single integer as parameter ( int ) and return nothing ( void ) and do not modify the contents of the class in anyway ( const )
*/
void executefunction ( int functionnumber, int inputforfunction )
{
if ( functionnumber < 0 || functionnumber > 2 ) return ;
( * functionpointers [ functionnumber ] ) ( inputforfunction ) ;
// this line calls the function which is pointed ( * ) to by the functionnumber-th element of the functionpointers array which is a member of the object pointed to by the current object pointer ( this ) and passes the function the value of the variable inputforfunction as a parameter
}
int main ( void )
{
functionpointers [ 0 ] = & square ;
functionpointers [ 1 ] = & cube ;
functionpointers [ 2 ] = & fourthpower ;
int count ;
for ( count = 0; count < 3; count ++ )
executefunction ( count, 2 ) ;
}
[-- Attachment #3: function-pointers-2.cpp --]
[-- Type: text/x-c++src, Size: 1448 bytes --]
#include "stdio.h"
inline int square ( int a ) { return a * a ; }
inline int cube ( int a ) { return a * a * a ; }
inline int fourthpower ( int a ) { return a * a * a * a ; }
inline int squarepluscube ( int a ) { return square ( a ) + cube ( a ) ; }
inline int squarepluscubeplusfourthpower ( int a ) { return squarepluscube ( a ) + fourthpower ( a ) ; }
int ( * functionpointers [ 5 ] ) ( int ) ;
/*
the above line declares an array called functionpointers that has three ( 3 ) elements which are all pointers ( * ) to functions which take a single integer as parameter ( int ) and return nothing ( void ) and do not modify the contents of the class in anyway ( const )
*/
void executefunction ( int functionnumber, int inputforfunction )
{
printf ( "%d\n", ( * functionpointers [ functionnumber ] ) ( inputforfunction ) ) ;
// this line calls the function which is pointed ( * ) to by the functionnumber-th element of the functionpointers array which is a member of the object pointed to by the current object pointer ( this ) and passes the function the value of the variable inputforfunction as a parameter
}
int main ( void )
{
functionpointers [ 0 ] = & square ;
functionpointers [ 1 ] = & cube ;
functionpointers [ 2 ] = & fourthpower ;
functionpointers [ 3 ] = & squarepluscube ;
functionpointers [ 4 ] = & squarepluscubeplusfourthpower ;
int count ;
for ( count = 0; count < 5; count ++ )
executefunction ( count, 2 ) ;
}
next reply other threads:[~2007-04-04 17:09 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-04 17:09 Shriramana Sharma [this message]
2007-04-04 18:23 ` Function pointers to inline functions Glynn Clements
2007-05-25 4:57 ` Shriramana Sharma
2007-05-25 22:41 ` Glynn Clements
2007-05-29 13:03 ` Shriramana Sharma
2007-06-04 7:08 ` Glynn Clements
2007-05-25 6:02 ` Shriramana Sharma
2007-04-04 18:33 ` Steve Graegert
2007-04-27 17:01 ` Shriramana Sharma
2007-05-04 13:40 ` Glynn Clements
2007-05-25 4:41 ` Shriramana Sharma
2007-05-25 22:45 ` Glynn Clements
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4613DBC2.8060708@gmail.com \
--to=samjnaa@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.