Typedef function pointer?
I'm learning how to dynamically load DLL's but what I don't understand is this line
typedef void (*FunctionFunc)();
I have a few questions. If someone is able answer them I would be grateful.
- Why is
typedef
used? - The syntax looks odd; after
void
should there not be a function name or something? It looks like an anonymous function. - Is a function pointer created to store the memory address of a function?
So I'm confused at the moment; can you clarify things for me?
c++ c pointers typedef
add a comment |
I'm learning how to dynamically load DLL's but what I don't understand is this line
typedef void (*FunctionFunc)();
I have a few questions. If someone is able answer them I would be grateful.
- Why is
typedef
used? - The syntax looks odd; after
void
should there not be a function name or something? It looks like an anonymous function. - Is a function pointer created to store the memory address of a function?
So I'm confused at the moment; can you clarify things for me?
c++ c pointers typedef
5
Take a look at the link (last section) learncpp.com/cpp-tutorial/78-function-pointers
– enthusiasticgeek
May 3 '13 at 3:28
6
Should be noted that since c++11using FunctionFunc = void (*)();
can be used instead. It is a bit more clear that you are just declaring a name for a type (pointer to function)
– user362515
Jan 8 '16 at 11:55
just to add to @user362515, a bit clearer form to me is:using FunctionFunc = void(void);
– topspin
May 28 '16 at 21:15
@topspin IIRC these two are not the same. One is a function pointer type, the other is function type. There is implicit conversion, that's why it works, IANA(C++)L so, one can step in and correct me. In any case, if the intend is to define a pointer type I think the syntax with the*
is a bit more explicit.
– user362515
May 31 '16 at 18:11
add a comment |
I'm learning how to dynamically load DLL's but what I don't understand is this line
typedef void (*FunctionFunc)();
I have a few questions. If someone is able answer them I would be grateful.
- Why is
typedef
used? - The syntax looks odd; after
void
should there not be a function name or something? It looks like an anonymous function. - Is a function pointer created to store the memory address of a function?
So I'm confused at the moment; can you clarify things for me?
c++ c pointers typedef
I'm learning how to dynamically load DLL's but what I don't understand is this line
typedef void (*FunctionFunc)();
I have a few questions. If someone is able answer them I would be grateful.
- Why is
typedef
used? - The syntax looks odd; after
void
should there not be a function name or something? It looks like an anonymous function. - Is a function pointer created to store the memory address of a function?
So I'm confused at the moment; can you clarify things for me?
c++ c pointers typedef
c++ c pointers typedef
edited Jul 26 '18 at 18:25
mrflash818
6621018
6621018
asked Nov 28 '10 at 4:50
Jack Harvin
2,32571820
2,32571820
5
Take a look at the link (last section) learncpp.com/cpp-tutorial/78-function-pointers
– enthusiasticgeek
May 3 '13 at 3:28
6
Should be noted that since c++11using FunctionFunc = void (*)();
can be used instead. It is a bit more clear that you are just declaring a name for a type (pointer to function)
– user362515
Jan 8 '16 at 11:55
just to add to @user362515, a bit clearer form to me is:using FunctionFunc = void(void);
– topspin
May 28 '16 at 21:15
@topspin IIRC these two are not the same. One is a function pointer type, the other is function type. There is implicit conversion, that's why it works, IANA(C++)L so, one can step in and correct me. In any case, if the intend is to define a pointer type I think the syntax with the*
is a bit more explicit.
– user362515
May 31 '16 at 18:11
add a comment |
5
Take a look at the link (last section) learncpp.com/cpp-tutorial/78-function-pointers
– enthusiasticgeek
May 3 '13 at 3:28
6
Should be noted that since c++11using FunctionFunc = void (*)();
can be used instead. It is a bit more clear that you are just declaring a name for a type (pointer to function)
– user362515
Jan 8 '16 at 11:55
just to add to @user362515, a bit clearer form to me is:using FunctionFunc = void(void);
– topspin
May 28 '16 at 21:15
@topspin IIRC these two are not the same. One is a function pointer type, the other is function type. There is implicit conversion, that's why it works, IANA(C++)L so, one can step in and correct me. In any case, if the intend is to define a pointer type I think the syntax with the*
is a bit more explicit.
– user362515
May 31 '16 at 18:11
5
5
Take a look at the link (last section) learncpp.com/cpp-tutorial/78-function-pointers
– enthusiasticgeek
May 3 '13 at 3:28
Take a look at the link (last section) learncpp.com/cpp-tutorial/78-function-pointers
– enthusiasticgeek
May 3 '13 at 3:28
6
6
Should be noted that since c++11
using FunctionFunc = void (*)();
can be used instead. It is a bit more clear that you are just declaring a name for a type (pointer to function)– user362515
Jan 8 '16 at 11:55
Should be noted that since c++11
using FunctionFunc = void (*)();
can be used instead. It is a bit more clear that you are just declaring a name for a type (pointer to function)– user362515
Jan 8 '16 at 11:55
just to add to @user362515, a bit clearer form to me is:
using FunctionFunc = void(void);
– topspin
May 28 '16 at 21:15
just to add to @user362515, a bit clearer form to me is:
using FunctionFunc = void(void);
– topspin
May 28 '16 at 21:15
@topspin IIRC these two are not the same. One is a function pointer type, the other is function type. There is implicit conversion, that's why it works, IANA(C++)L so, one can step in and correct me. In any case, if the intend is to define a pointer type I think the syntax with the
*
is a bit more explicit.– user362515
May 31 '16 at 18:11
@topspin IIRC these two are not the same. One is a function pointer type, the other is function type. There is implicit conversion, that's why it works, IANA(C++)L so, one can step in and correct me. In any case, if the intend is to define a pointer type I think the syntax with the
*
is a bit more explicit.– user362515
May 31 '16 at 18:11
add a comment |
5 Answers
5
active
oldest
votes
typedef
is a language construct that associates a name to a type.
You use it the same way you would use the original type, for instance
typedef int myinteger;
typedef char *mystring;
typedef void (*myfunc)();
using them like
myinteger i; // is equivalent to int i;
mystring s; // is the same as char *s;
myfunc f; // compile equally as void (*f)();
As you can see, you could just replace the typedefed name with its definition given above.
The difficulty lies in the pointer to functions syntax and readability in C and C++, and the typedef
can improve the readability of such declarations. However, the syntax is appropriate, since functions - unlike other simpler types - may have a return value and parameters, thus the sometimes lengthy and complex declaration of a pointer to function.
The readability may start to be really tricky with pointers to functions arrays, and some other even more indirect flavors.
To answer your three questions
Why is typedef used?
To ease the reading of the code - especially for pointers to functions, or structure names.The syntax looks odd (in the pointer to function declaration)
That syntax is not obvious to read, at least when beginning. Using atypedef
declaration instead eases the readingIs a function pointer created to store the memory address of a function?
Yes, a function pointer stores the address of a function. This has nothing to do with thetypedef
construct which only ease the writing/reading of a program ; the compiler just expands the typedef definition before compiling the actual code.
Example:
typedef int (*t_somefunc)(int,int);
int product(int u, int v) {
return u*v;
}
t_somefunc afunc = &product;
...
int x2 = (*afunc)(123, 456); // call product() to calculate 123*456
5
in the last example, wouldn't just 'square' refer to the same thing i.e pointer to the function instead of using &square.
– pranavk
Mar 5 '13 at 13:32
2
Question, in your first typedef example you have of the formtypedef type alias
but with function pointers there only seems to be 2 arguments,typedef type
. Is alias defaulted to the name specified in type argument?
– dchhetri
May 3 '13 at 3:56
5
@user814628: It is not clear quite what you're asking. Withtypedef int newname
, you are makingnewname
into an alias forint
. Withtypedef int (*func)(int)
, you are makingfunc
into an alias forint (*)(int)
— a pointer to function taking anint
argument and returning anint
value.
– Jonathan Leffler
May 3 '13 at 4:01
10
I guess I'm just confused about the ordering. With typedefint (*func)(int)
, I understand that func is an alias, just a little confused because the alias is tangled with the type. Going bytypedef int INT
as an example I would be more of ease if typedef function pointer was of formtypedef int(*function)(int) FUNC_1
. That way I can see the type and alias in two separate token instead of being meshed into one.
– dchhetri
May 3 '13 at 5:07
2
Regarding "Is a function pointer created to store the memory address of a function? Yes, ", no, not in this code. Also, stating that the name introduced by atypedef
is a "keyword" is incorrect. And regarding the evaluation "the syntax is appropriate", both the C and C++ language creators disagree, calling it a failed experiment.
– Cheers and hth. - Alf
Mar 24 '15 at 11:16
|
show 4 more comments
typedef
is used to alias types; in this case you're aliasingFunctionFunc
tovoid(*)()
.
Indeed the syntax does look odd, have a look at this:
typedef void (*FunctionFunc) ( );
// ^ ^ ^
// return type type name arguments
No, this simply tells the compiler that the
FunctionFunc
type will be a function pointer, it doesn't define one, like this:
FunctionFunc x;
void doSomething() { printf("Hello theren"); }
x = &doSomething;
x(); //prints "Hello there"
23
typedef
does not declare a new type. you can have manytypedef
-defined names of the same type, and they are not distinct (e.g. wrt. overloading of functions). there are some circumstances in which, with respect to how you can use the name, atypedef
-defined name is not exactly equivalent to what it's defined as, but multipletypedef
-defined names for the same, are equivalent.
– Cheers and hth. - Alf
Nov 28 '10 at 5:00
Ah i get it now. Thanks.
– Jack Harvin
Nov 28 '10 at 5:09
@Jack, You're welcome! :)
– Jacob Relkin
Nov 28 '10 at 5:09
4
+1 for the answer to question 2 - very clear. The rest of the answers were clear too, but that one stands out for me :-)
– Michael van der Westhuizen
Jan 19 '12 at 18:57
add a comment |
Without the typedef
word, in C++ the declaration would declare a variable FunctionFunc
of type pointer to function of no arguments, returning void
.
With the typedef
it instead defines FunctionFunc
as a name for that type.
4
This is a really good way to think about it. If you read the other answers carefully, they do not really address the OP's confusion about the entanglement of the alias and name. I learned something new from reading this post.
– Mad Physicist
Apr 7 '15 at 20:51
add a comment |
If you can use C++11 you may want to use std::function
and using
keyword.
using FunctionFunc = std::function<void(int arg1, std::string arg2)>;
add a comment |
#include <stdio.h>
#include <math.h>
/*
To define a new type name with typedef, follow these steps:
1. Write the statement as if a variable of the desired type were being declared.
2. Where the name of the declared variable would normally appear, substitute the new type name.
3. In front of everything, place the keyword typedef.
*/
// typedef a primitive data type
typedef double distance;
// typedef struct
typedef struct{
int x;
int y;
} point;
//typedef an array
typedef point points[100];
points ps = {0}; // ps is an array of 100 point
// typedef a function
typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point)
// prototype a function
distance findDistance(point, point);
int main(int argc, char const *argv)
{
// delcare a function pointer
distanceFun_p func_p;
// initialize the function pointer with a function address
func_p = findDistance;
// initialize two point variables
point p1 = {0,0} , p2 = {1,1};
// call the function through the pointer
distance d = func_p(p1,p2);
printf("the distance is %fn", d );
return 0;
}
distance findDistance(point p1, point p2)
{
distance xdiff = p1.x - p2.x;
distance ydiff = p1.y - p2.y;
return sqrt( (xdiff * xdiff) + (ydiff * ydiff) );
}
1
Under what circumstances is the '&' needed? For example, do 'func_p = &findDistance' and 'func_p = findDistance' work equally well?
– Donna
Oct 30 '17 at 14:06
1
A function name is a pointer, so you do not need to use '&' with it. In other words, '&' is optional when function pointers are considered. However, for primitive data type you need to use the '&' to get its address.
– Amjad
Oct 30 '17 at 16:41
1
It has just always struck as odd that the '&' can ever be optional. If a typedef is used to create a pointer to a primitive (i.e.typedef (*double) p
, is the '&' optional?
– Donna
Oct 31 '17 at 17:39
I think you wanted to typetypedef double* p
to define a pointer to a double. If you want to populate thep
pointer from a primitive variable, you will need to use the '&'. A side note, we you *<pointer name> to dereference a pointer. The*
is used in the function pointer to dereference the pointer (function name) and go to the block of memory where the function instructions are located.
– Amjad
Oct 31 '17 at 20:16
add a comment |
protected by Yu Hao Oct 3 '13 at 6:56
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
typedef
is a language construct that associates a name to a type.
You use it the same way you would use the original type, for instance
typedef int myinteger;
typedef char *mystring;
typedef void (*myfunc)();
using them like
myinteger i; // is equivalent to int i;
mystring s; // is the same as char *s;
myfunc f; // compile equally as void (*f)();
As you can see, you could just replace the typedefed name with its definition given above.
The difficulty lies in the pointer to functions syntax and readability in C and C++, and the typedef
can improve the readability of such declarations. However, the syntax is appropriate, since functions - unlike other simpler types - may have a return value and parameters, thus the sometimes lengthy and complex declaration of a pointer to function.
The readability may start to be really tricky with pointers to functions arrays, and some other even more indirect flavors.
To answer your three questions
Why is typedef used?
To ease the reading of the code - especially for pointers to functions, or structure names.The syntax looks odd (in the pointer to function declaration)
That syntax is not obvious to read, at least when beginning. Using atypedef
declaration instead eases the readingIs a function pointer created to store the memory address of a function?
Yes, a function pointer stores the address of a function. This has nothing to do with thetypedef
construct which only ease the writing/reading of a program ; the compiler just expands the typedef definition before compiling the actual code.
Example:
typedef int (*t_somefunc)(int,int);
int product(int u, int v) {
return u*v;
}
t_somefunc afunc = &product;
...
int x2 = (*afunc)(123, 456); // call product() to calculate 123*456
5
in the last example, wouldn't just 'square' refer to the same thing i.e pointer to the function instead of using &square.
– pranavk
Mar 5 '13 at 13:32
2
Question, in your first typedef example you have of the formtypedef type alias
but with function pointers there only seems to be 2 arguments,typedef type
. Is alias defaulted to the name specified in type argument?
– dchhetri
May 3 '13 at 3:56
5
@user814628: It is not clear quite what you're asking. Withtypedef int newname
, you are makingnewname
into an alias forint
. Withtypedef int (*func)(int)
, you are makingfunc
into an alias forint (*)(int)
— a pointer to function taking anint
argument and returning anint
value.
– Jonathan Leffler
May 3 '13 at 4:01
10
I guess I'm just confused about the ordering. With typedefint (*func)(int)
, I understand that func is an alias, just a little confused because the alias is tangled with the type. Going bytypedef int INT
as an example I would be more of ease if typedef function pointer was of formtypedef int(*function)(int) FUNC_1
. That way I can see the type and alias in two separate token instead of being meshed into one.
– dchhetri
May 3 '13 at 5:07
2
Regarding "Is a function pointer created to store the memory address of a function? Yes, ", no, not in this code. Also, stating that the name introduced by atypedef
is a "keyword" is incorrect. And regarding the evaluation "the syntax is appropriate", both the C and C++ language creators disagree, calling it a failed experiment.
– Cheers and hth. - Alf
Mar 24 '15 at 11:16
|
show 4 more comments
typedef
is a language construct that associates a name to a type.
You use it the same way you would use the original type, for instance
typedef int myinteger;
typedef char *mystring;
typedef void (*myfunc)();
using them like
myinteger i; // is equivalent to int i;
mystring s; // is the same as char *s;
myfunc f; // compile equally as void (*f)();
As you can see, you could just replace the typedefed name with its definition given above.
The difficulty lies in the pointer to functions syntax and readability in C and C++, and the typedef
can improve the readability of such declarations. However, the syntax is appropriate, since functions - unlike other simpler types - may have a return value and parameters, thus the sometimes lengthy and complex declaration of a pointer to function.
The readability may start to be really tricky with pointers to functions arrays, and some other even more indirect flavors.
To answer your three questions
Why is typedef used?
To ease the reading of the code - especially for pointers to functions, or structure names.The syntax looks odd (in the pointer to function declaration)
That syntax is not obvious to read, at least when beginning. Using atypedef
declaration instead eases the readingIs a function pointer created to store the memory address of a function?
Yes, a function pointer stores the address of a function. This has nothing to do with thetypedef
construct which only ease the writing/reading of a program ; the compiler just expands the typedef definition before compiling the actual code.
Example:
typedef int (*t_somefunc)(int,int);
int product(int u, int v) {
return u*v;
}
t_somefunc afunc = &product;
...
int x2 = (*afunc)(123, 456); // call product() to calculate 123*456
5
in the last example, wouldn't just 'square' refer to the same thing i.e pointer to the function instead of using &square.
– pranavk
Mar 5 '13 at 13:32
2
Question, in your first typedef example you have of the formtypedef type alias
but with function pointers there only seems to be 2 arguments,typedef type
. Is alias defaulted to the name specified in type argument?
– dchhetri
May 3 '13 at 3:56
5
@user814628: It is not clear quite what you're asking. Withtypedef int newname
, you are makingnewname
into an alias forint
. Withtypedef int (*func)(int)
, you are makingfunc
into an alias forint (*)(int)
— a pointer to function taking anint
argument and returning anint
value.
– Jonathan Leffler
May 3 '13 at 4:01
10
I guess I'm just confused about the ordering. With typedefint (*func)(int)
, I understand that func is an alias, just a little confused because the alias is tangled with the type. Going bytypedef int INT
as an example I would be more of ease if typedef function pointer was of formtypedef int(*function)(int) FUNC_1
. That way I can see the type and alias in two separate token instead of being meshed into one.
– dchhetri
May 3 '13 at 5:07
2
Regarding "Is a function pointer created to store the memory address of a function? Yes, ", no, not in this code. Also, stating that the name introduced by atypedef
is a "keyword" is incorrect. And regarding the evaluation "the syntax is appropriate", both the C and C++ language creators disagree, calling it a failed experiment.
– Cheers and hth. - Alf
Mar 24 '15 at 11:16
|
show 4 more comments
typedef
is a language construct that associates a name to a type.
You use it the same way you would use the original type, for instance
typedef int myinteger;
typedef char *mystring;
typedef void (*myfunc)();
using them like
myinteger i; // is equivalent to int i;
mystring s; // is the same as char *s;
myfunc f; // compile equally as void (*f)();
As you can see, you could just replace the typedefed name with its definition given above.
The difficulty lies in the pointer to functions syntax and readability in C and C++, and the typedef
can improve the readability of such declarations. However, the syntax is appropriate, since functions - unlike other simpler types - may have a return value and parameters, thus the sometimes lengthy and complex declaration of a pointer to function.
The readability may start to be really tricky with pointers to functions arrays, and some other even more indirect flavors.
To answer your three questions
Why is typedef used?
To ease the reading of the code - especially for pointers to functions, or structure names.The syntax looks odd (in the pointer to function declaration)
That syntax is not obvious to read, at least when beginning. Using atypedef
declaration instead eases the readingIs a function pointer created to store the memory address of a function?
Yes, a function pointer stores the address of a function. This has nothing to do with thetypedef
construct which only ease the writing/reading of a program ; the compiler just expands the typedef definition before compiling the actual code.
Example:
typedef int (*t_somefunc)(int,int);
int product(int u, int v) {
return u*v;
}
t_somefunc afunc = &product;
...
int x2 = (*afunc)(123, 456); // call product() to calculate 123*456
typedef
is a language construct that associates a name to a type.
You use it the same way you would use the original type, for instance
typedef int myinteger;
typedef char *mystring;
typedef void (*myfunc)();
using them like
myinteger i; // is equivalent to int i;
mystring s; // is the same as char *s;
myfunc f; // compile equally as void (*f)();
As you can see, you could just replace the typedefed name with its definition given above.
The difficulty lies in the pointer to functions syntax and readability in C and C++, and the typedef
can improve the readability of such declarations. However, the syntax is appropriate, since functions - unlike other simpler types - may have a return value and parameters, thus the sometimes lengthy and complex declaration of a pointer to function.
The readability may start to be really tricky with pointers to functions arrays, and some other even more indirect flavors.
To answer your three questions
Why is typedef used?
To ease the reading of the code - especially for pointers to functions, or structure names.The syntax looks odd (in the pointer to function declaration)
That syntax is not obvious to read, at least when beginning. Using atypedef
declaration instead eases the readingIs a function pointer created to store the memory address of a function?
Yes, a function pointer stores the address of a function. This has nothing to do with thetypedef
construct which only ease the writing/reading of a program ; the compiler just expands the typedef definition before compiling the actual code.
Example:
typedef int (*t_somefunc)(int,int);
int product(int u, int v) {
return u*v;
}
t_somefunc afunc = &product;
...
int x2 = (*afunc)(123, 456); // call product() to calculate 123*456
edited Jun 9 '16 at 8:59
answered Nov 28 '10 at 5:13
Ring Ø
21.4k45081
21.4k45081
5
in the last example, wouldn't just 'square' refer to the same thing i.e pointer to the function instead of using &square.
– pranavk
Mar 5 '13 at 13:32
2
Question, in your first typedef example you have of the formtypedef type alias
but with function pointers there only seems to be 2 arguments,typedef type
. Is alias defaulted to the name specified in type argument?
– dchhetri
May 3 '13 at 3:56
5
@user814628: It is not clear quite what you're asking. Withtypedef int newname
, you are makingnewname
into an alias forint
. Withtypedef int (*func)(int)
, you are makingfunc
into an alias forint (*)(int)
— a pointer to function taking anint
argument and returning anint
value.
– Jonathan Leffler
May 3 '13 at 4:01
10
I guess I'm just confused about the ordering. With typedefint (*func)(int)
, I understand that func is an alias, just a little confused because the alias is tangled with the type. Going bytypedef int INT
as an example I would be more of ease if typedef function pointer was of formtypedef int(*function)(int) FUNC_1
. That way I can see the type and alias in two separate token instead of being meshed into one.
– dchhetri
May 3 '13 at 5:07
2
Regarding "Is a function pointer created to store the memory address of a function? Yes, ", no, not in this code. Also, stating that the name introduced by atypedef
is a "keyword" is incorrect. And regarding the evaluation "the syntax is appropriate", both the C and C++ language creators disagree, calling it a failed experiment.
– Cheers and hth. - Alf
Mar 24 '15 at 11:16
|
show 4 more comments
5
in the last example, wouldn't just 'square' refer to the same thing i.e pointer to the function instead of using &square.
– pranavk
Mar 5 '13 at 13:32
2
Question, in your first typedef example you have of the formtypedef type alias
but with function pointers there only seems to be 2 arguments,typedef type
. Is alias defaulted to the name specified in type argument?
– dchhetri
May 3 '13 at 3:56
5
@user814628: It is not clear quite what you're asking. Withtypedef int newname
, you are makingnewname
into an alias forint
. Withtypedef int (*func)(int)
, you are makingfunc
into an alias forint (*)(int)
— a pointer to function taking anint
argument and returning anint
value.
– Jonathan Leffler
May 3 '13 at 4:01
10
I guess I'm just confused about the ordering. With typedefint (*func)(int)
, I understand that func is an alias, just a little confused because the alias is tangled with the type. Going bytypedef int INT
as an example I would be more of ease if typedef function pointer was of formtypedef int(*function)(int) FUNC_1
. That way I can see the type and alias in two separate token instead of being meshed into one.
– dchhetri
May 3 '13 at 5:07
2
Regarding "Is a function pointer created to store the memory address of a function? Yes, ", no, not in this code. Also, stating that the name introduced by atypedef
is a "keyword" is incorrect. And regarding the evaluation "the syntax is appropriate", both the C and C++ language creators disagree, calling it a failed experiment.
– Cheers and hth. - Alf
Mar 24 '15 at 11:16
5
5
in the last example, wouldn't just 'square' refer to the same thing i.e pointer to the function instead of using &square.
– pranavk
Mar 5 '13 at 13:32
in the last example, wouldn't just 'square' refer to the same thing i.e pointer to the function instead of using &square.
– pranavk
Mar 5 '13 at 13:32
2
2
Question, in your first typedef example you have of the form
typedef type alias
but with function pointers there only seems to be 2 arguments, typedef type
. Is alias defaulted to the name specified in type argument?– dchhetri
May 3 '13 at 3:56
Question, in your first typedef example you have of the form
typedef type alias
but with function pointers there only seems to be 2 arguments, typedef type
. Is alias defaulted to the name specified in type argument?– dchhetri
May 3 '13 at 3:56
5
5
@user814628: It is not clear quite what you're asking. With
typedef int newname
, you are making newname
into an alias for int
. With typedef int (*func)(int)
, you are making func
into an alias for int (*)(int)
— a pointer to function taking an int
argument and returning an int
value.– Jonathan Leffler
May 3 '13 at 4:01
@user814628: It is not clear quite what you're asking. With
typedef int newname
, you are making newname
into an alias for int
. With typedef int (*func)(int)
, you are making func
into an alias for int (*)(int)
— a pointer to function taking an int
argument and returning an int
value.– Jonathan Leffler
May 3 '13 at 4:01
10
10
I guess I'm just confused about the ordering. With typedef
int (*func)(int)
, I understand that func is an alias, just a little confused because the alias is tangled with the type. Going by typedef int INT
as an example I would be more of ease if typedef function pointer was of form typedef int(*function)(int) FUNC_1
. That way I can see the type and alias in two separate token instead of being meshed into one.– dchhetri
May 3 '13 at 5:07
I guess I'm just confused about the ordering. With typedef
int (*func)(int)
, I understand that func is an alias, just a little confused because the alias is tangled with the type. Going by typedef int INT
as an example I would be more of ease if typedef function pointer was of form typedef int(*function)(int) FUNC_1
. That way I can see the type and alias in two separate token instead of being meshed into one.– dchhetri
May 3 '13 at 5:07
2
2
Regarding "Is a function pointer created to store the memory address of a function? Yes, ", no, not in this code. Also, stating that the name introduced by a
typedef
is a "keyword" is incorrect. And regarding the evaluation "the syntax is appropriate", both the C and C++ language creators disagree, calling it a failed experiment.– Cheers and hth. - Alf
Mar 24 '15 at 11:16
Regarding "Is a function pointer created to store the memory address of a function? Yes, ", no, not in this code. Also, stating that the name introduced by a
typedef
is a "keyword" is incorrect. And regarding the evaluation "the syntax is appropriate", both the C and C++ language creators disagree, calling it a failed experiment.– Cheers and hth. - Alf
Mar 24 '15 at 11:16
|
show 4 more comments
typedef
is used to alias types; in this case you're aliasingFunctionFunc
tovoid(*)()
.
Indeed the syntax does look odd, have a look at this:
typedef void (*FunctionFunc) ( );
// ^ ^ ^
// return type type name arguments
No, this simply tells the compiler that the
FunctionFunc
type will be a function pointer, it doesn't define one, like this:
FunctionFunc x;
void doSomething() { printf("Hello theren"); }
x = &doSomething;
x(); //prints "Hello there"
23
typedef
does not declare a new type. you can have manytypedef
-defined names of the same type, and they are not distinct (e.g. wrt. overloading of functions). there are some circumstances in which, with respect to how you can use the name, atypedef
-defined name is not exactly equivalent to what it's defined as, but multipletypedef
-defined names for the same, are equivalent.
– Cheers and hth. - Alf
Nov 28 '10 at 5:00
Ah i get it now. Thanks.
– Jack Harvin
Nov 28 '10 at 5:09
@Jack, You're welcome! :)
– Jacob Relkin
Nov 28 '10 at 5:09
4
+1 for the answer to question 2 - very clear. The rest of the answers were clear too, but that one stands out for me :-)
– Michael van der Westhuizen
Jan 19 '12 at 18:57
add a comment |
typedef
is used to alias types; in this case you're aliasingFunctionFunc
tovoid(*)()
.
Indeed the syntax does look odd, have a look at this:
typedef void (*FunctionFunc) ( );
// ^ ^ ^
// return type type name arguments
No, this simply tells the compiler that the
FunctionFunc
type will be a function pointer, it doesn't define one, like this:
FunctionFunc x;
void doSomething() { printf("Hello theren"); }
x = &doSomething;
x(); //prints "Hello there"
23
typedef
does not declare a new type. you can have manytypedef
-defined names of the same type, and they are not distinct (e.g. wrt. overloading of functions). there are some circumstances in which, with respect to how you can use the name, atypedef
-defined name is not exactly equivalent to what it's defined as, but multipletypedef
-defined names for the same, are equivalent.
– Cheers and hth. - Alf
Nov 28 '10 at 5:00
Ah i get it now. Thanks.
– Jack Harvin
Nov 28 '10 at 5:09
@Jack, You're welcome! :)
– Jacob Relkin
Nov 28 '10 at 5:09
4
+1 for the answer to question 2 - very clear. The rest of the answers were clear too, but that one stands out for me :-)
– Michael van der Westhuizen
Jan 19 '12 at 18:57
add a comment |
typedef
is used to alias types; in this case you're aliasingFunctionFunc
tovoid(*)()
.
Indeed the syntax does look odd, have a look at this:
typedef void (*FunctionFunc) ( );
// ^ ^ ^
// return type type name arguments
No, this simply tells the compiler that the
FunctionFunc
type will be a function pointer, it doesn't define one, like this:
FunctionFunc x;
void doSomething() { printf("Hello theren"); }
x = &doSomething;
x(); //prints "Hello there"
typedef
is used to alias types; in this case you're aliasingFunctionFunc
tovoid(*)()
.
Indeed the syntax does look odd, have a look at this:
typedef void (*FunctionFunc) ( );
// ^ ^ ^
// return type type name arguments
No, this simply tells the compiler that the
FunctionFunc
type will be a function pointer, it doesn't define one, like this:
FunctionFunc x;
void doSomething() { printf("Hello theren"); }
x = &doSomething;
x(); //prints "Hello there"
edited Jul 26 '18 at 17:42
neilxdims
50059
50059
answered Nov 28 '10 at 4:57
Jacob Relkin
134k24313300
134k24313300
23
typedef
does not declare a new type. you can have manytypedef
-defined names of the same type, and they are not distinct (e.g. wrt. overloading of functions). there are some circumstances in which, with respect to how you can use the name, atypedef
-defined name is not exactly equivalent to what it's defined as, but multipletypedef
-defined names for the same, are equivalent.
– Cheers and hth. - Alf
Nov 28 '10 at 5:00
Ah i get it now. Thanks.
– Jack Harvin
Nov 28 '10 at 5:09
@Jack, You're welcome! :)
– Jacob Relkin
Nov 28 '10 at 5:09
4
+1 for the answer to question 2 - very clear. The rest of the answers were clear too, but that one stands out for me :-)
– Michael van der Westhuizen
Jan 19 '12 at 18:57
add a comment |
23
typedef
does not declare a new type. you can have manytypedef
-defined names of the same type, and they are not distinct (e.g. wrt. overloading of functions). there are some circumstances in which, with respect to how you can use the name, atypedef
-defined name is not exactly equivalent to what it's defined as, but multipletypedef
-defined names for the same, are equivalent.
– Cheers and hth. - Alf
Nov 28 '10 at 5:00
Ah i get it now. Thanks.
– Jack Harvin
Nov 28 '10 at 5:09
@Jack, You're welcome! :)
– Jacob Relkin
Nov 28 '10 at 5:09
4
+1 for the answer to question 2 - very clear. The rest of the answers were clear too, but that one stands out for me :-)
– Michael van der Westhuizen
Jan 19 '12 at 18:57
23
23
typedef
does not declare a new type. you can have many typedef
-defined names of the same type, and they are not distinct (e.g. wrt. overloading of functions). there are some circumstances in which, with respect to how you can use the name, a typedef
-defined name is not exactly equivalent to what it's defined as, but multiple typedef
-defined names for the same, are equivalent.– Cheers and hth. - Alf
Nov 28 '10 at 5:00
typedef
does not declare a new type. you can have many typedef
-defined names of the same type, and they are not distinct (e.g. wrt. overloading of functions). there are some circumstances in which, with respect to how you can use the name, a typedef
-defined name is not exactly equivalent to what it's defined as, but multiple typedef
-defined names for the same, are equivalent.– Cheers and hth. - Alf
Nov 28 '10 at 5:00
Ah i get it now. Thanks.
– Jack Harvin
Nov 28 '10 at 5:09
Ah i get it now. Thanks.
– Jack Harvin
Nov 28 '10 at 5:09
@Jack, You're welcome! :)
– Jacob Relkin
Nov 28 '10 at 5:09
@Jack, You're welcome! :)
– Jacob Relkin
Nov 28 '10 at 5:09
4
4
+1 for the answer to question 2 - very clear. The rest of the answers were clear too, but that one stands out for me :-)
– Michael van der Westhuizen
Jan 19 '12 at 18:57
+1 for the answer to question 2 - very clear. The rest of the answers were clear too, but that one stands out for me :-)
– Michael van der Westhuizen
Jan 19 '12 at 18:57
add a comment |
Without the typedef
word, in C++ the declaration would declare a variable FunctionFunc
of type pointer to function of no arguments, returning void
.
With the typedef
it instead defines FunctionFunc
as a name for that type.
4
This is a really good way to think about it. If you read the other answers carefully, they do not really address the OP's confusion about the entanglement of the alias and name. I learned something new from reading this post.
– Mad Physicist
Apr 7 '15 at 20:51
add a comment |
Without the typedef
word, in C++ the declaration would declare a variable FunctionFunc
of type pointer to function of no arguments, returning void
.
With the typedef
it instead defines FunctionFunc
as a name for that type.
4
This is a really good way to think about it. If you read the other answers carefully, they do not really address the OP's confusion about the entanglement of the alias and name. I learned something new from reading this post.
– Mad Physicist
Apr 7 '15 at 20:51
add a comment |
Without the typedef
word, in C++ the declaration would declare a variable FunctionFunc
of type pointer to function of no arguments, returning void
.
With the typedef
it instead defines FunctionFunc
as a name for that type.
Without the typedef
word, in C++ the declaration would declare a variable FunctionFunc
of type pointer to function of no arguments, returning void
.
With the typedef
it instead defines FunctionFunc
as a name for that type.
edited Mar 24 '15 at 13:57
BartoszKP
26.7k1065103
26.7k1065103
answered Nov 28 '10 at 4:58
Cheers and hth. - Alf
123k10158264
123k10158264
4
This is a really good way to think about it. If you read the other answers carefully, they do not really address the OP's confusion about the entanglement of the alias and name. I learned something new from reading this post.
– Mad Physicist
Apr 7 '15 at 20:51
add a comment |
4
This is a really good way to think about it. If you read the other answers carefully, they do not really address the OP's confusion about the entanglement of the alias and name. I learned something new from reading this post.
– Mad Physicist
Apr 7 '15 at 20:51
4
4
This is a really good way to think about it. If you read the other answers carefully, they do not really address the OP's confusion about the entanglement of the alias and name. I learned something new from reading this post.
– Mad Physicist
Apr 7 '15 at 20:51
This is a really good way to think about it. If you read the other answers carefully, they do not really address the OP's confusion about the entanglement of the alias and name. I learned something new from reading this post.
– Mad Physicist
Apr 7 '15 at 20:51
add a comment |
If you can use C++11 you may want to use std::function
and using
keyword.
using FunctionFunc = std::function<void(int arg1, std::string arg2)>;
add a comment |
If you can use C++11 you may want to use std::function
and using
keyword.
using FunctionFunc = std::function<void(int arg1, std::string arg2)>;
add a comment |
If you can use C++11 you may want to use std::function
and using
keyword.
using FunctionFunc = std::function<void(int arg1, std::string arg2)>;
If you can use C++11 you may want to use std::function
and using
keyword.
using FunctionFunc = std::function<void(int arg1, std::string arg2)>;
answered Sep 21 '17 at 8:20
Halil Kaskavalci
7361025
7361025
add a comment |
add a comment |
#include <stdio.h>
#include <math.h>
/*
To define a new type name with typedef, follow these steps:
1. Write the statement as if a variable of the desired type were being declared.
2. Where the name of the declared variable would normally appear, substitute the new type name.
3. In front of everything, place the keyword typedef.
*/
// typedef a primitive data type
typedef double distance;
// typedef struct
typedef struct{
int x;
int y;
} point;
//typedef an array
typedef point points[100];
points ps = {0}; // ps is an array of 100 point
// typedef a function
typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point)
// prototype a function
distance findDistance(point, point);
int main(int argc, char const *argv)
{
// delcare a function pointer
distanceFun_p func_p;
// initialize the function pointer with a function address
func_p = findDistance;
// initialize two point variables
point p1 = {0,0} , p2 = {1,1};
// call the function through the pointer
distance d = func_p(p1,p2);
printf("the distance is %fn", d );
return 0;
}
distance findDistance(point p1, point p2)
{
distance xdiff = p1.x - p2.x;
distance ydiff = p1.y - p2.y;
return sqrt( (xdiff * xdiff) + (ydiff * ydiff) );
}
1
Under what circumstances is the '&' needed? For example, do 'func_p = &findDistance' and 'func_p = findDistance' work equally well?
– Donna
Oct 30 '17 at 14:06
1
A function name is a pointer, so you do not need to use '&' with it. In other words, '&' is optional when function pointers are considered. However, for primitive data type you need to use the '&' to get its address.
– Amjad
Oct 30 '17 at 16:41
1
It has just always struck as odd that the '&' can ever be optional. If a typedef is used to create a pointer to a primitive (i.e.typedef (*double) p
, is the '&' optional?
– Donna
Oct 31 '17 at 17:39
I think you wanted to typetypedef double* p
to define a pointer to a double. If you want to populate thep
pointer from a primitive variable, you will need to use the '&'. A side note, we you *<pointer name> to dereference a pointer. The*
is used in the function pointer to dereference the pointer (function name) and go to the block of memory where the function instructions are located.
– Amjad
Oct 31 '17 at 20:16
add a comment |
#include <stdio.h>
#include <math.h>
/*
To define a new type name with typedef, follow these steps:
1. Write the statement as if a variable of the desired type were being declared.
2. Where the name of the declared variable would normally appear, substitute the new type name.
3. In front of everything, place the keyword typedef.
*/
// typedef a primitive data type
typedef double distance;
// typedef struct
typedef struct{
int x;
int y;
} point;
//typedef an array
typedef point points[100];
points ps = {0}; // ps is an array of 100 point
// typedef a function
typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point)
// prototype a function
distance findDistance(point, point);
int main(int argc, char const *argv)
{
// delcare a function pointer
distanceFun_p func_p;
// initialize the function pointer with a function address
func_p = findDistance;
// initialize two point variables
point p1 = {0,0} , p2 = {1,1};
// call the function through the pointer
distance d = func_p(p1,p2);
printf("the distance is %fn", d );
return 0;
}
distance findDistance(point p1, point p2)
{
distance xdiff = p1.x - p2.x;
distance ydiff = p1.y - p2.y;
return sqrt( (xdiff * xdiff) + (ydiff * ydiff) );
}
1
Under what circumstances is the '&' needed? For example, do 'func_p = &findDistance' and 'func_p = findDistance' work equally well?
– Donna
Oct 30 '17 at 14:06
1
A function name is a pointer, so you do not need to use '&' with it. In other words, '&' is optional when function pointers are considered. However, for primitive data type you need to use the '&' to get its address.
– Amjad
Oct 30 '17 at 16:41
1
It has just always struck as odd that the '&' can ever be optional. If a typedef is used to create a pointer to a primitive (i.e.typedef (*double) p
, is the '&' optional?
– Donna
Oct 31 '17 at 17:39
I think you wanted to typetypedef double* p
to define a pointer to a double. If you want to populate thep
pointer from a primitive variable, you will need to use the '&'. A side note, we you *<pointer name> to dereference a pointer. The*
is used in the function pointer to dereference the pointer (function name) and go to the block of memory where the function instructions are located.
– Amjad
Oct 31 '17 at 20:16
add a comment |
#include <stdio.h>
#include <math.h>
/*
To define a new type name with typedef, follow these steps:
1. Write the statement as if a variable of the desired type were being declared.
2. Where the name of the declared variable would normally appear, substitute the new type name.
3. In front of everything, place the keyword typedef.
*/
// typedef a primitive data type
typedef double distance;
// typedef struct
typedef struct{
int x;
int y;
} point;
//typedef an array
typedef point points[100];
points ps = {0}; // ps is an array of 100 point
// typedef a function
typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point)
// prototype a function
distance findDistance(point, point);
int main(int argc, char const *argv)
{
// delcare a function pointer
distanceFun_p func_p;
// initialize the function pointer with a function address
func_p = findDistance;
// initialize two point variables
point p1 = {0,0} , p2 = {1,1};
// call the function through the pointer
distance d = func_p(p1,p2);
printf("the distance is %fn", d );
return 0;
}
distance findDistance(point p1, point p2)
{
distance xdiff = p1.x - p2.x;
distance ydiff = p1.y - p2.y;
return sqrt( (xdiff * xdiff) + (ydiff * ydiff) );
}
#include <stdio.h>
#include <math.h>
/*
To define a new type name with typedef, follow these steps:
1. Write the statement as if a variable of the desired type were being declared.
2. Where the name of the declared variable would normally appear, substitute the new type name.
3. In front of everything, place the keyword typedef.
*/
// typedef a primitive data type
typedef double distance;
// typedef struct
typedef struct{
int x;
int y;
} point;
//typedef an array
typedef point points[100];
points ps = {0}; // ps is an array of 100 point
// typedef a function
typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point)
// prototype a function
distance findDistance(point, point);
int main(int argc, char const *argv)
{
// delcare a function pointer
distanceFun_p func_p;
// initialize the function pointer with a function address
func_p = findDistance;
// initialize two point variables
point p1 = {0,0} , p2 = {1,1};
// call the function through the pointer
distance d = func_p(p1,p2);
printf("the distance is %fn", d );
return 0;
}
distance findDistance(point p1, point p2)
{
distance xdiff = p1.x - p2.x;
distance ydiff = p1.y - p2.y;
return sqrt( (xdiff * xdiff) + (ydiff * ydiff) );
}
answered Jan 11 '17 at 22:24
Amjad
757813
757813
1
Under what circumstances is the '&' needed? For example, do 'func_p = &findDistance' and 'func_p = findDistance' work equally well?
– Donna
Oct 30 '17 at 14:06
1
A function name is a pointer, so you do not need to use '&' with it. In other words, '&' is optional when function pointers are considered. However, for primitive data type you need to use the '&' to get its address.
– Amjad
Oct 30 '17 at 16:41
1
It has just always struck as odd that the '&' can ever be optional. If a typedef is used to create a pointer to a primitive (i.e.typedef (*double) p
, is the '&' optional?
– Donna
Oct 31 '17 at 17:39
I think you wanted to typetypedef double* p
to define a pointer to a double. If you want to populate thep
pointer from a primitive variable, you will need to use the '&'. A side note, we you *<pointer name> to dereference a pointer. The*
is used in the function pointer to dereference the pointer (function name) and go to the block of memory where the function instructions are located.
– Amjad
Oct 31 '17 at 20:16
add a comment |
1
Under what circumstances is the '&' needed? For example, do 'func_p = &findDistance' and 'func_p = findDistance' work equally well?
– Donna
Oct 30 '17 at 14:06
1
A function name is a pointer, so you do not need to use '&' with it. In other words, '&' is optional when function pointers are considered. However, for primitive data type you need to use the '&' to get its address.
– Amjad
Oct 30 '17 at 16:41
1
It has just always struck as odd that the '&' can ever be optional. If a typedef is used to create a pointer to a primitive (i.e.typedef (*double) p
, is the '&' optional?
– Donna
Oct 31 '17 at 17:39
I think you wanted to typetypedef double* p
to define a pointer to a double. If you want to populate thep
pointer from a primitive variable, you will need to use the '&'. A side note, we you *<pointer name> to dereference a pointer. The*
is used in the function pointer to dereference the pointer (function name) and go to the block of memory where the function instructions are located.
– Amjad
Oct 31 '17 at 20:16
1
1
Under what circumstances is the '&' needed? For example, do 'func_p = &findDistance' and 'func_p = findDistance' work equally well?
– Donna
Oct 30 '17 at 14:06
Under what circumstances is the '&' needed? For example, do 'func_p = &findDistance' and 'func_p = findDistance' work equally well?
– Donna
Oct 30 '17 at 14:06
1
1
A function name is a pointer, so you do not need to use '&' with it. In other words, '&' is optional when function pointers are considered. However, for primitive data type you need to use the '&' to get its address.
– Amjad
Oct 30 '17 at 16:41
A function name is a pointer, so you do not need to use '&' with it. In other words, '&' is optional when function pointers are considered. However, for primitive data type you need to use the '&' to get its address.
– Amjad
Oct 30 '17 at 16:41
1
1
It has just always struck as odd that the '&' can ever be optional. If a typedef is used to create a pointer to a primitive (i.e.
typedef (*double) p
, is the '&' optional?– Donna
Oct 31 '17 at 17:39
It has just always struck as odd that the '&' can ever be optional. If a typedef is used to create a pointer to a primitive (i.e.
typedef (*double) p
, is the '&' optional?– Donna
Oct 31 '17 at 17:39
I think you wanted to type
typedef double* p
to define a pointer to a double. If you want to populate the p
pointer from a primitive variable, you will need to use the '&'. A side note, we you *<pointer name> to dereference a pointer. The *
is used in the function pointer to dereference the pointer (function name) and go to the block of memory where the function instructions are located.– Amjad
Oct 31 '17 at 20:16
I think you wanted to type
typedef double* p
to define a pointer to a double. If you want to populate the p
pointer from a primitive variable, you will need to use the '&'. A side note, we you *<pointer name> to dereference a pointer. The *
is used in the function pointer to dereference the pointer (function name) and go to the block of memory where the function instructions are located.– Amjad
Oct 31 '17 at 20:16
add a comment |
protected by Yu Hao Oct 3 '13 at 6:56
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
5
Take a look at the link (last section) learncpp.com/cpp-tutorial/78-function-pointers
– enthusiasticgeek
May 3 '13 at 3:28
6
Should be noted that since c++11
using FunctionFunc = void (*)();
can be used instead. It is a bit more clear that you are just declaring a name for a type (pointer to function)– user362515
Jan 8 '16 at 11:55
just to add to @user362515, a bit clearer form to me is:
using FunctionFunc = void(void);
– topspin
May 28 '16 at 21:15
@topspin IIRC these two are not the same. One is a function pointer type, the other is function type. There is implicit conversion, that's why it works, IANA(C++)L so, one can step in and correct me. In any case, if the intend is to define a pointer type I think the syntax with the
*
is a bit more explicit.– user362515
May 31 '16 at 18:11