function pointers and function pointers array 
Author Message
 function pointers and function pointers array

I am doing a project and having a problem, in the following
code:

typedef void  MyFunc(int);
MyFunc  *myFunc_1(int), *myFunc_2(int), *myFunc_3(int);

they are in my header file, and what I want to do is
to have some functions that each of them
returns a pointer to a function and this
function returns nothing. For example
myFunc_1(int) returns a pointer to MyFunc data(or function)
type, and this function type returns void.

From the FAQ, I know that the function name without
a parameter list works just like a pointer, like,
if myFunc_1(int) is a function, then myFunc_1 is a pointer
to this function, so I do a sizeof(myFunc_1) in my main
function, then the compiler says,

cannot take size of function: myFunc_1

the reason why I am confused is that if I do

double somedouble;
sizeof(&somedouble);

it works fine, so why pointer to a function is different
from a pointer to, say, a double? They are both the addresses
of some data and the sizeof them should be fixed for specific
system, isn't it?

Also, I then want to make a array of function pointers, this
is related to the above compiler errors. I can understand
that we can't have an array of functions, because during
the compling time, the size of the functions are unknown and`
therefore it's impossible to allocate memory for this array,
but I still believe an array of POINTERS to functions could
be exist, the following code in my header file

MyFunc array_func[]={
        myFunc_1,
        myFunc_2,
        myFunc_3

Quote:
};

I did this because I believe myFunc_1, etc, are pointers
to functions, but not functions themselves, but the
compiler seems don't mind the difference, and again,
it says,

cannot make array of functions.

I have been blocked here for 2 days and hope you gurus
give me some hints, please email me although follow up
is fine, thanks.

QD



Fri, 22 Nov 1996 06:50:43 GMT  
 function pointers and function pointers array
Because of the high priority of the () [function call] "operator",
pointers to functions should be typedef'd as follows:

typedef <return type> (*funcp_type)(<parameters>);

--
Miguel Carrasquer         ____________________  ~~~
Amsterdam                [                  ||]~  



Fri, 22 Nov 1996 08:02:46 GMT  
 function pointers and function pointers array

writes:

Quote:
>Distribution: comp.lang.c comp.std.c

(The distribution header line is not the right place to list
newsgroups.  comp.lang.c is the appropriate group anyway.)

Quote:
>typedef void  MyFunc(int);

As someone else has already noted, you may mean:

        typedef void (*MyFunc)(int);

Assuming this is not the case (which is then consistent with the
rest of your article, i.e., the previous answer is probably wrong),
you should remember that `MyFunc' is an alias for the type:

        function (of 1 arg of type int) returning void

Quote:
>MyFunc      *myFunc_1(int), *myFunc_2(int), *myFunc_3(int);

This declares three functions, each of which has:

        function (of 1 arg of type int) returning pointer to MyFunc

which is just shorthand for:

        function (int) returning pointer to
                function (int) returning void

Quote:
>From the FAQ, I know that the function name without
>a parameter list works just like a pointer, like,
>if myFunc_1(int) is a function, then myFunc_1 is a pointer
>to this function ....

This is not quite true, and perhaps the answer to question 10.9
should be reworded.  In particular, array and function names decay
to pointers only in `value contexts'.  All C expressions sit in
value contexts EXCEPT:

    - the left hand sides of assignment operators;
    - targets of a ++ or -- pre- or post-increment or -decrement;
    - operands of the `sizeof' operator.

Quote:
>so I do a sizeof(myFunc_1) in my main function, then the compiler says,
>cannot take size of function: myFunc_1

This is correct, because the operand of `sizeof' is not in a value
context and therefore the transformation from `function returning
T' to `pointer to function returning T' does not occur.  In order
to obtain the size of the type `pointer to function returning T',
you must hand `sizeof' an expression that already has that type,
or use the alternative syntax (`sizeof(T (*)())').

Quote:
>Also, I then want to make a array of function pointers, this
>is related to the above compiler errors.

Actually, it is independent of the above.  Given any function
type F (and the typedef for `MyFunc' has such a type), one can
construct the type `array of unknown size of pointer to F' by
writing:

        F (*identifier)[]

Quote:
>MyFunc array_func[]={

Here, as you can see, the parentheses and asterisk are missing.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc (+1 510 549 1145)



Fri, 22 Nov 1996 12:20:55 GMT  
 function pointers and function pointers array

Quote:

>... All C expressions sit in value contexts EXCEPT:

>    - the left hand sides of assignment operators;
>    - targets of a ++ or -- pre- or post-increment or -decrement;
>    - operands of the `sizeof' operator.

Oops, I left one out: the target of a unary `&' operator is not
in a value context.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc (+1 510 549 1145)



Sat, 23 Nov 1996 08:29:18 GMT  
 function pointers and function pointers array

Quote:
>>... All C expressions sit in value contexts EXCEPT:
>Oops, I left one out: the target of a unary `&' operator is not
>in a value context.

As it turns out, I left more than one out; neither the original nor
the augmented list should be considered complete.  (Additional non-
value contexts include the left side of a structure selection `.'
operator, and, depending on one's point of view, string literals
used as array initializers.  I think this may be complete at last,
but... :-) )


out.)
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc (+1 510 549 1145)



Fri, 29 Nov 1996 16:18:10 GMT  
 
 [ 5 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. Pointer to an array of pointers to functions

4. Pointer to an array of function pointers [HELP!]

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

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

7. Pointer to function returning pointer to function returning...

8. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

9. Pointers to Structure that contains pointer to Function

10. Casting function pointer to void pointer

11. Assigning structure pointer to function pointer

12. pointers to functions and pointers to void.

 

 
Powered by phpBB® Forum Software