
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)