Re: typedef with function pointers



vardhan <vardhanw@xxxxxxxxx> writes:

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?

We can, but the pointers are the problem. For example:

typedef int XFunc(void);

defines XFunc to be a name for the type "function of no parameters
returning int". There is an exact equivalence between this and the
code you posted to typedef a "struct X".

If you want a typdef for a pointer to a "struct X" we just add a star,
because the syntax works:

typedef struct X { ... } *XStructPtr;

but you can't just add a star in front of XFunc above because in that
case the start will be associated with the return type of the
function:

typedef int *XFunc(void);

This is a synonym for "function of no parameters returning a pointer
to int". To alter this association, we need brackets:

typedef int (*XFuncPtr)(void);

Now XFuncPtr is "pointer to function of no parameters returning an
int". Of course. given the first simple typedef, we can use it to
define the second more easily:

typedef int XFunc(void);
typedef XFunc *XFuncPtr;

and many people prefer to do this for complex point to function (and
array) types.

Or is it that the placement of the type
defined follows a similar rule for both these declarations?

I could not understand that. I hope you did not mean what I just said
at great length!

--
Ben.
.



Relevant Pages

  • Re: Question about C Functions
    ... This is a pointer to an int-returning-function whose sole parameter is ... typedef int; ...
    (comp.lang.c)
  • Re: Memory Allocating problem
    ... > typedef struct _NAME_INFO { ... typedef struct NAME_INFO {// don't use leading underscore ... Just use NAME_INFO* when you need a pointer. ... You're allocating p incorrectly. ...
    (comp.lang.c)
  • Re: Typdef pointers to structs or not? [from clc]
    ... Thus a typedef is essential. ... Therefore, only a pointer to ... the struct is useful for the abstract type. ... about optimizing const struct foo* is a red herring, ...
    (comp.std.c)
  • Re: Opaque pointers
    ... struct, and not the fact that it has been typedefed to a pointer. ... and the additional typedef is not relevant to the opacity. ... But the typedef is just a pointer. ...
    (comp.lang.c)
  • Re: Memory Allocating problem
    ... Some argue that just using "struct foo" directly is clearer ... than using a typedef name. ... I definitely wouldn't create a typedef for a pointer to a struct ... want to allocate the size of the struct. ...
    (comp.lang.c)