449,412 Members | 1,006 Online Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

# typedef with function pointers

 P: n/a Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc())[])(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int (*(*(*ptr2d_fptr)()))(); ptr2d_fptr q; 4. typedef char (*(*arr_fptr)()); arr_fptr x; 5. typedef float *(*(*(*ptr_fptr)()))(); 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 Replies

 P: n/a Googy wrote: > Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc())[])(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int (*(*(*ptr2d_fptr)()))(); ptr2d_fptr q; 4. typedef char (*(*arr_fptr)()); arr_fptr x; 5. typedef float *(*(*(*ptr_fptr)()))(); 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. -- Posted via a free Usenet account from http://www.teranews.com Sep 30 '07 #2

 P: n/a 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)()); arr2d_ptr p; 3. typedef int (*(*(*ptr2d_fptr)()))(); ptr2d_fptr q; 4. typedef char (*(*arr_fptr)()); arr_fptr x; 5. typedef float *(*(*(*ptr_fptr)()))(); 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 "To this day, many C programmers believe that 'strong typing' just means pounding extra hard on the keyboard" Sep 30 '07 #3

 P: n/a On Sep 30, 1:52 pm, Tor Rustad -- Tor "To this day, many C programmers believe that 'strong typing' just means pounding extra hard on the keyboard" Sep 30 '07 #4

 P: n/a vardhan 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)()); arr2d_ptr p; 3. typedef int (*(*(*ptr2d_fptr)()))(); ptr2d_fptr q; 4. typedef char (*(*arr_fptr)()); arr_fptr x; 5. typedef float *(*(*(*ptr_fptr)()))(); 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

 P: n/a vardhan wrote: On Sep 30, 1:52 pm, Tor Rustad 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)()); arr2d_ptr p;3. typedef int (*(*(*ptr2d_fptr)()))(); ptr2d_fptr q;4. typedef char (*(*arr_fptr)()); arr_fptr x;5. typedef float *(*(*(*ptr_fptr)()))(); 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;' 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

 P: n/a On Sep 30, 11:39 pm, Eric Sosman

 P: n/a Googy wrote: On Sep 30, 11:39 pm, Eric Sosman

 P: n/a On Sep 30, 11:31 pm, Ben Bacarisse 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

 P: n/a On Sep 30, 11:39 pm, Eric Sosman > 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;' 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

 P: n/a vardhan wrote On 10/04/07 12:11,: On Sep 30, 11:39 pm, Eric Sosman [...] When you write `int f(double x);' you say "f(42.0) isan int, hence f is a function taking a double and returningan int." When you write `int (*f)(double x);' you say"(*f)(42.0) is an int, hence f is a pointer to a functiontaking a double and returning an int." Again, the positionof f with respect to the other elements of the declarationis 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

 P: n/a vardhan vardhan 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 parametersreturning int". There is an exact equivalence between this and thecode 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

 P: n/a On Fri, 05 Oct 2007 16:38:47 +0100, in comp.lang.c , Ben Bacarisse Is it possible to use it in the code (similar to how a functionpointer can be used by taking the address of a function)? Can I nowdeclare 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 discussion thread is closed

Replies have been disabled for this discussion. 