Reputation: 55
I am studying pointers of C and from the book of Deitel I don't get the difference between int(*function)(int,int)
and int*function(int,int)
when the function is expressed.
Upvotes: 1
Views: 10928
Reputation: 1586
Rule of thumb for reading types in C:
Starting with the identifier you're defining
in int(*function)(int,int)
, "function
is a..."
in int*function(int,int)
, "function
is a..."
Read to the right until you hit the end of the line or a closing parenthesis
in int(*function)(int,int)
, you hit the parenthesis immediately.
in int*function(int,int)
, "... function that takes two parameters of type int
and int
..."
Read left from where you started
in int(*function)(int,int)
, "... pointer to ..."
in int*function(int,int)
, "... that returns a pointer to int
."
If you stopped because you hit a closing parenthesis, follow steps 2 & 3 again starting with the closing parenthesis and returning to its corresponding opening parenthesis.
in int(*function)(int,int)
we only read (*function)
so far, so we continue: "... function that takes parameters of type int
and int
..." and backing up, "... that returns an int
"
we hit the end of the line in the other one.
Putting it all together:
int(*function)(int,int)
function
is a pointer to a function that takes two arguments of type int
and int
that returns an int
int*function(int,int)
function
is a function that takes two arguments of type int
and int
and returns a pointer to int
.
Upvotes: 7
Reputation:
The first one is a pointer to function which received two int
arguments and the second one is just a function which returns pointer to int and receives two int
arguments. It is two really different programming entities. First is data type second is code (function).
Upvotes: 3
Reputation: 26184
The first is a pointer to a function which returns int
. The second is a declaration of a function which returns a pointer to int
.
Upvotes: 2