Need the pointer to a function 
Author Message
 Need the pointer to a function

I can't seem to retrieve the pointer to a function.  It returns
something that doesn't seem random, but I cant make out what.

As I was writing this message, it struck me.  On the location that is
returned, a jump to the actual function can be found.  Is this the
same on a UNIX machine (I am using Windows)?  And how do I pass the
arguments to the function?  Or call the thing alltogether?

I am programming on a Windows machine, but it's being build for a UNIX
machine, and I might want to get this working on a Mac as well.

Can anybody tell me the best way to get the pointer to a function and
then call it using this pointer?

This is some sample code I used:

void testFunction1(int i);

void testFunction2(int i);

int main(void)
{
        void*   lpFunc;

        lpFunc = testFunction1;
        lpFunc = testFunction2;

        testFunction1(0);
        testFunction2(1);

        return 0;

Quote:
}

void testFunction1(int i)
{
        i = 1;
        i += 1;

Quote:
}

void testFunction2(int i)
{
        i = 2;
Quote:
}

--



Wed, 31 Jul 2002 03:00:00 GMT  
 Need the pointer to a function

Quote:

> void testFunction1(int i);
>         void*   lpFunc;
>         lpFunc = testFunction1;
>         testFunction1(0);

Your main mistake is in trying to use an inappropriate pointer type.
        void (*lpfunc)(int); // ptr to func taking int returning void
--



Wed, 31 Jul 2002 03:00:00 GMT  
 Need the pointer to a function

Quote:
> I can't seem to retrieve the pointer to a function.  It returns
> something that doesn't seem random, but I cant make out what.

> As I was writing this message, it struck me.  On the location that is
> returned, a jump to the actual function can be found.  Is this the
> same on a UNIX machine (I am using Windows)?  And how do I pass the
> arguments to the function?  Or call the thing alltogether?

> I am programming on a Windows machine, but it's being build for a UNIX
> machine, and I might want to get this working on a Mac as well.

> Can anybody tell me the best way to get the pointer to a function and
> then call it using this pointer?

The best way is to declare your pointer as a pointer to a function, as
below:
Quote:

> void testFunction1(int i);
> void testFunction2(int i);

> int main(void)
> {
> void* lpFunc;

void (*lpFunc)( int );  /* This definition means that lpFunc is a */
         /* pointer to a function that takes an int parameter */
         /* and returns nothing */
Quote:
> lpFunc = testFunction1;
> lpFunc = testFunction2;

> testFunction1(0);
> testFunction2(1);

  lpFunc(0);  /* since you wanted to use your pointer */

Quote:
> return 0;
> }

> void testFunction1(int i)
> {
> i = 1;

Hmm, what's a point of this?  Or, is this just random scribbling?

Quote:
> i += 1;
> }

> void testFunction2(int i)
> {
> i = 2;
> }

Then, it will work portably on all platforms

--
poncho
--



Wed, 31 Jul 2002 03:00:00 GMT  
 Need the pointer to a function

Quote:

> I can't seem to retrieve the pointer to a function.  It returns
> something that doesn't seem random, but I cant make out what.
> Can anybody tell me the best way to get the pointer to a function and
> then call it using this pointer?
> void testFunction1(int i);
> void testFunction2(int i);
> int main(void)
> {
>    void*   lpFunc;

Here's the problem.  void * is guaranteed to be able to hold a pointer to
any object, but functions are not objects.  You should use the declaration

        void (*lpFunc)(int);

instead.

--
Eric Amick
Columbia, MD

--



Thu, 01 Aug 2002 03:00:00 GMT  
 Need the pointer to a function


Quote:
>Can anybody tell me the best way to get the pointer to a function and
>then call it using this pointer?

>This is some sample code I used:

But what do you think this code does?

Quote:

>void testFunction1(int i);

>void testFunction2(int i);

OK, two prototypes.

Quote:

>int main(void)
>{
>       void*   lpFunc;

wrong type, actually very wrong because there are no requirements placed
on void* as regards storing function pointers. try:
        void (*lpFunc)(int);
Quote:

>       lpFunc = testFunction1;
>       lpFunc = testFunction2;

now lpfunc contains a pointer to testFunction2.  You never used it when
it pointed to testFunction1, and never use the new value.
try writing:
        lpFunc(0);
to test your code (but make sure that the functions produce observable
behaviour.

Quote:

>       testFunction1(0);
call testFunction1
>       testFunction2(1);
call testFunction2

>       return 0;
>}

>void testFunction1(int i)
>{
>       i = 1;
>       i += 1;
>}

>void testFunction2(int i)
>{
>       i = 2;
>}

I cannot find any output to any of your functions so how do you know
that they do not do what you have specified?

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Thu, 01 Aug 2002 03:00:00 GMT  
 Need the pointer to a function
On Sun, 13 Feb 2000 07:08:34 GMT, Francis Glassborow

Quote:



>>Can anybody tell me the best way to get the pointer to a function and
>>then call it using this pointer?

>>This is some sample code I used:

>But what do you think this code does?

[snip code]

>I cannot find any output to any of your functions so how do you know
>that they do not do what you have specified?

Stepping thru it in a de{*filter*}?
Avoids debug output code.

Quote:
>Francis Glassborow      Journal Editor, Association of C & C++ Users
>64 Southfield Rd
>Oxford OX4 1PA          +44(0)1865 246490
>All opinions are mine and do not represent those of any organisation

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada
--

                                use address above to reply
--



Fri, 16 Aug 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How to use (pointer to function), and function and pointer to (pointer to function)

2. Pointer Functions and Pointers to Pointer Functions

3. Question about signal()/pointers to functions that return pointers to functions

4. function pointers and function pointers array

5. C++ function pointers versus C function pointer problem

6. Pointer to function returning pointer to function returning...

7. Need help for writing a function returning pointer

8. Need help with these function variable pointers?

9. HELP NEEDED : passing pointers to functions

10. Need ex. of pointer to function

11. function pointer help needed (again)

12. Function pointer array help needed

 

 
Powered by phpBB® Forum Software