Re: typedef with function pointers



vardhan wrote:
On Sep 30, 1:52 pm, Tor Rustad <tor_rus...@xxxxxxxxxxx> wrote:
Googy wrote:
Hi!!
Can any one explain me the meaning of following notations clearly :
1. typedef char(*(*frpapfrc())[])();
frpapfrc f;
2. typedef int (*(arr2d_ptr)())[3][4];
arr2d_ptr p;
3. typedef int (*(*(*ptr2d_fptr)())[10])();
ptr2d_fptr q;
4. typedef char (*(*arr_fptr[3])())[10];
arr_fptr x;
5. typedef float *(*(*(*ptr_fptr)())[10])();
ptr_fptr y;
What are f,p,q,x,y?? and how?
Please don't just answer what they are explain then clearly..
homework-o-meter = 100%

Hint: cdecl

Heres are quick question:

A typical typedef for a data type like a struct goes like:

typedef struct X { ...} XStruct;

where XStruct is now "struct X". Why don't we follow a similar way to
define function pointers? Or is it that the placement of the type
defined follows a similar rule for both these declarations?

Forget about the typedef for a moment, and recall C's
practice that "declaration mimics use." When you write
`int x;' you say "x is an int." When you write `int *x;'
you say "*x is an int, hence x is a pointer to int." When
you write `int x[42];' you say "x[index] is an int, hence
x is an array of int. The position of x with respect to
the other elements of the declaration depends on the way
x would be used in an expression: The type comes first,
followed by a sort of "expression template" involving the
identifier that's being declared.

When you write `int f(double x);' you say "f(42.0) is
an int, hence f is a function taking a double and returning
an int." When you write `int (*f)(double x);' you say
"(*f)(42.0) is an int, hence f is a pointer to a function
taking a double and returning an int." Again, the position
of f with respect to the other elements of the declaration
is the position f would have in an actual expression.

Now re-insert the typedef. Syntactically, this is just
like inserting static or extern or register or auto, and does
not affect what goes on in the rest of the declaration. The
others specify where the new identifier's object is to be
stored; typedef says "It's not really an object at all, but
an alias for the type that the object would have had if the
typedef keyword hadn't been there."

int x; /* x is an int; make one */
extern int x; /* x is an int residing elsewhere */
typedef int x; /* x is an alias for int */

int (*f)(double); /* f is a function ptr; make one */
extern int (*f)(double); /* f is a function ptr elsewhere */
typedef int (*f)(double); /* f is an alias for function ptr */

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Re: const qualifier and VC6.0
    ... > emphasis is on types, while in C the emphasis is on expressions. ... When declaring pointer and reference variables, ... int &p; ... For example, the following declaration ...
    (comp.lang.cpp)
  • Re: how to call a variable without reference in c
    ... fuction with out sending as a argument, pointer and can't declare as ... int a=10; ... Not in his declaration it didn't. ...
    (comp.lang.c)
  • Re: formal parameter 1 different from declaration?
    ... Here is the typedef one: ... typedef LR (HWND, long, int, int); ... struct tagWnd; // forward declaration ... takes a struct tagHwnd and the other takes a long. ...
    (microsoft.public.vc.language)
  • Re: Porting from visual C to gcc problem
    ... the compiler must have come across the typedef ... >>function declaration, ... then the scope lasts to through the body to ... >>int func; ...
    (comp.lang.c.moderated)
  • Re: typedef function pointer
    ... the semantics for typedef is: ... That's the syntax, not the semantics. ... to be an array of 20 ints, the declaration is ... to a function taking to int arguments and returning it, ...
    (comp.lang.c)