Re: typedef with function pointers
- From: Martin Wells <warint@xxxxxxxxxx>
- Date: Sun, 30 Sep 2007 11:32:58 -0700
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
.
- References:
- typedef with function pointers
- From: Googy
- typedef with function pointers
- Prev by Date: Re: typedef with function pointers
- Next by Date: Re: Should we broaden the topicality of this group?
- Previous by thread: Re: typedef with function pointers
- Next by thread: C program Questions
- Index(es):
Relevant Pages
|