Re: typedef with function pointers



Googy:

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..
Thanks in advance...


Given this looks like a homework question, I'll explain how it works
rather than giving you the answer.

First off, typedef has the same syntax as defining variables. For
instance:

int arr[5];

The above defines an array called "arr" with five int elements.

typedef int arr[5];

The above declares a type called "arr" which is an array of five int
elements.

If you're trying to break a complicated declaration down, then start
with the name. I'm not familiar with C99, so I'm going to change the
first declaration a little for it to be C89:

typedef char(*(*frpapfrc(void))[5])(void);

The stuff right beside the name tells you that firstly:
1: It's a function that takes no arguments
2: That returns a pointer

Now take that stuff out and you're left with:

typedef char(*frpapfrc[5])(void);

This tells us that:
3: It's an array of five elements
4: Each element is a pointer

Again, strip that stuff out:

typedef char frpapfrc (void);

5: This is a function with no parameters that returns a char.

Now just put them in order:

A function that takes no arguments, and returns a pointer to an array
of five pointers to functions which take no arguments and which return
a char.

You can test it out as follows:

typedef char(*(*frpapfrc(void))[5])(void);

typedef char A(void);

typedef A *B[5];

typedef B *C;

typedef C D(void);



int main(void)
{
D *p1 = 0;
frpapfrc *p2 = p1; /* Hopefully we don't get a type mis-match */

return 0;
}


Martin

.



Relevant Pages

  • Re: RE:How to realize the OOP in C?
    ... > int n; ... be a pointer to a standard structure like: ... typedef struct object_of_fubar * ObjectOfFubarP; ... Note that the class structure is allocated only ...
    (comp.lang.c)
  • Re: "Portability" contructs like UINT32 etc.
    ... > typedef char CHAR; ... > typedef int INT32; ... > typedef char* PCHAR; ... Both processors had hardware alignment requirements. ...
    (comp.lang.c)
  • Re: I dont understand typedef example
    ... * Using typedef, declare 'func' to have type ... 'function taking two int arguments, ... declared ptr as a pointer object that can points to a function of the ...
    (comp.lang.c)
  • Re: strang function pointer from faq book (help)
    ... > pointer to a function which is typedef ed. ... It is just an ordinary typedef. ... then MyJ gets an alias for the whole data type, in this case for 'int'. ...
    (comp.lang.cpp)
  • Re: typedef with function pointers
    ... typedef has the same syntax as defining variables. ... The above defines an array called "arr" with five int elements. ... Each element is a pointer ... typedef char frpapfrc; ...
    (comp.lang.c)