473,233 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

typedef with function pointers

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

Gaurav

Sep 30 '07 #1
12 4585
Googy wrote:
>
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?
They are objects of the type declared. For what that type is, see
the preceding typedef.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 30 '07 #2
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

--
Tor <torust [at] online [dot] no>
"To this day, many C programmers believe that 'strong typing' just means
pounding extra hard on the keyboard"
Sep 30 '07 #3
On Sep 30, 1:52 pm, Tor Rustad <tor_rus...@hotmail.comwrote:
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?

-Vardhan

>
--
Tor <torust [at] online [dot] no>
"To this day, many C programmers believe that 'strong typing' just means
pounding extra hard on the keyboard"

Sep 30 '07 #4
vardhan <va******@gmail.comwrites:
On Sep 30, 1:52 pm, Tor Rustad <tor_rus...@hotmail.comwrote:
>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.
Sep 30 '07 #5
vardhan wrote:
On Sep 30, 1:52 pm, Tor Rustad <tor_rus...@hotmail.comwrote:
>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
es*****@ieee-dot-org.invalid
Sep 30 '07 #6
On Sep 30, 11:39 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
vardhan wrote:
On Sep 30, 1:52 pm, Tor Rustad <tor_rus...@hotmail.comwrote:
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
esos...@ieee-dot-org.invalid
Thanks Eric for your explanation it was a concept told in a new way
which helped me to solve the problems further, Thanks very much once
again!!

Oct 1 '07 #7
Googy wrote:
On Sep 30, 11:39 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
[large snip]
--
Eric Sosman
esos...@ieee-dot-org.invalid

Thanks Eric for your explanation it was a concept told in a new way
which helped me to solve the problems further, Thanks very much once
again!!
Please trim your posts to leave just enough quoted material for
context. In particular, snip the signatures, the parts following "-- "
(I've left the example above). A good newsreader would do that for you.
Alas, you use Google Groups, so no help there.


Brian
Oct 1 '07 #8
On Sep 30, 11:31 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
vardhan<vardh...@gmail.comwrites:
<snip>
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".
I have not seen this kind of typedef for a function name before.
Thanks.
Is it possible to use it in the code (similar to how a function
pointer can be used by taking the address of a function)? Can I now
declare a "variable" of type XFunc and initialize it - how?

e.g.

int func1(int i);
typedef int XFunc(int);
typedef int (*XFuncPtr)(int);

XFunc myFunc = funcl; // correct??
XFuncPtr myFuncPtr = &func1;
>
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.
Thanks for the explanation.
>
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!
In a way yes, but you answered my question!

-Vardhan
>
--
Ben.

Oct 4 '07 #9
On Sep 30, 11:39 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
vardhanwrote:
On Sep 30, 1:52 pm, Tor Rustad <tor_rus...@hotmail.comwrote:
Googy wrote:
<snip>
>
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.
A question: here, as f, by definition, is a variable of type pointer
to a function which takes double and returns an int, I assume that it
can be used as such (without a typedef) to hold the address of a
function of that type. So if I do not use a typedef, I can just use:

int (*f)(double);

to define a variable f which is of the type explained above.
If I were to initialize this I would something like:

int func1(double) { return 0;}

f = &func1;

My question is, can I initialize the variable f in the declaration
itself.

i.e. as for any normal variable I could do:

int x = 5;

how can I do this for a variable which is a 1. Function pointer 2.
Function.

1. Function pointer:

int (*f)(double y) = &func1; // this should be correct.
int (*f1)(double y) { return 0; } // Is this allowed?

2. Function:

int (f)(double) = func1; // is this allowed?
int (f)(double) { return 0;} // Im actually just defining a function,
but I can also think
// of it as a variable of type function
which is declared and
// initialized at the same time! Am I
correct?

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 */
I think my question about the equivalence of the typedefs for struct
and function pointers got answered. Thanks!

-Vardhan
--
Eric Sosman
esos...@ieee-dot-org.invalid

Oct 4 '07 #10
vardhan wrote On 10/04/07 12:11,:
On Sep 30, 11:39 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
>[...]
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.

A question: here, as f, by definition, is a variable of type pointer
to a function which takes double and returns an int, I assume that it
can be used as such (without a typedef) to hold the address of a
function of that type. So if I do not use a typedef, I can just use:

int (*f)(double);

to define a variable f which is of the type explained above.
If I were to initialize this I would something like:

int func1(double) { return 0;}

f = &func1;

My question is, can I initialize the variable f in the declaration
itself.
Yes.

int func1(double x) { return 0; }

int (*f)(double) = func1;

Or another example, this time with just a function
declaration instead of a full-dress definition:

#include <math.h/* declares atan2() and pow() */

double (*f2)(double, double) = atan2;
if (trig_confuses_me)
f2 = pow;

--
Er*********@sun.com
Oct 4 '07 #11
vardhan <va******@gmail.comwrites:
On Sep 30, 11:31 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>vardhan<vardh...@gmail.comwrites:
<snip>
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".

I have not seen this kind of typedef for a function name before.
Thanks.
Is it possible to use it in the code (similar to how a function
pointer can be used by taking the address of a function)? Can I now
declare a "variable" of type XFunc and initialize it - how?
You can't. This is because variables can have function type. They
can, at best, be pointers to functions:

XFunc *fp = some_function_name;

You can use this sort of typedef to declare (but not define) a
function. For example, if you have code that has some operation type
defined as, say, a function taking two ints and returning an int you
can write this sort of code:

typedef int Operation(int, int);

extern Operation op_add, op_sub, op_mul, op_div;

struct expression {
struct expression *e1, *e2;
Operation *op;
};

...
struct expression *ep = malloc(sizeof *ep);
if (ep) {
ep->op = op_mul;
...
}

--
Ben.
Oct 5 '07 #12
On Fri, 05 Oct 2007 16:38:47 +0100, in comp.lang.c , Ben Bacarisse
<be********@bsb.me.ukwrote:
>Is it possible to use it in the code (similar to how a function
pointer can be used by taking the address of a function)? Can I now
declare a "variable" of type XFunc and initialize it - how?

You can't. This is because variables can have function type.
You mean "can't" in the above.
>They can, at best, be pointers to functions:
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 5 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
14
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further...
10
by: Barbrawl McBribe | last post by:
Is is possible to use typedefs to cast function pointers? I think I saw this in the WINGs src; grep for '(hashFunc)'. So far, trying to use a typedef to cast function pointers so that a return...
5
by: Cancerbero | last post by:
Hi (first, excuse me for my bad english) As I know, the semantics for typedef is: typedef A B; I think this makes B a synonym of A, where A is an existing data type. Is that right? Based...
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
2
by: Enrico Porreca | last post by:
Is the following a legal typedef? (gcc -Wall -ansi -pedantic says it's ok) typedef void Callback(void *obj); I mean "a Callback is a function taking void * and returning void" (not "a pointer...
2
by: Mehta Shailendrakumar | last post by:
Hi all, Have alook at two different declarations below: typedef unsigned char uc; typedef void (*fptr) (void); The first declaration has a left part which is a C keyword: "unsigned char"...
14
by: yang__lee | last post by:
Hi, You all know typedef typedef struct g { int a; int b; } google;
16
by: mdh | last post by:
A quick ? :-) question about Typedefs. There is a very brief discussion about this in K&R ( p146). Googling this group, there is a surprising dearth of questions about these. From one of the...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.