How to read this variable declaration? - c

int (*(*q)(int (*)()))();
Ok, I start with: q is a pointer to a function, which takes... Not sure what should follow next, but perhaps it's ...a pointer to function, which takes nothing and returns int, and returns pointer to a function, which takes nothing and returns int.

The trick is that q itself is a function pointer that returns and takes a function pointer. cdecl says:
declare q as pointer to function (pointer to function returning int) returning pointer to function returning int

Find the lefmost identifier, then work your way out remembering that *a[] is an array of pointer, (*a)[] is a pointer to an array, *f() is a function returning a pointer, and (*f)() is a pointer to a function. Remember that in a prototype you only need to provide the type of the parameter; int f( int ); declares f as a function that takes a single int parameter; int f( int * ); declares f as a function that takes a single int pointer as a parameter. Similarly, int f( int (*)[N] ); declares f as a function that takes a pointer to an array as a parameter. Apply these rules recursively to any parameters in the function.
Thus:
q -- q
*q -- is a pointer to
(*q)( ) -- a function
(*q)( * ) -- that takes a pointer to
(*q)( (*)()) -- a function
(*q)(int (*)()) -- returning int
*(*q)(int (*)()) -- returning a pointer to
(*(*q)(int (*)()))() -- a function
int (*(*q)(int (*)()))(); -- returning int

You are right.
q is a pointer point to a function passing an pointer to a function (passing nothing returning int) returning a pointer to a function (passing nothing returning int).
See here.
http://c-faq.com/decl/spiral.anderson.html

According to Precedence and associativity of Operators in the C Programming language.
You can understand it in the following steps:
int (*(*q)(int (*)()))();
q->*->(int (*)())->*->()->int
1 2 3 4 5
1:q is a pointer
2:q is a function pointer, the function it points to has a parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int.
3:q is a function pointer, the function it pointers to has parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int. And
the function which q points to has a return type : pointer.
4:q is a function pointer, the function it pointers to has parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int. And
the function which q points to has a return type : pointer(this pointer also points to a function which has no parameter).
5:q is a function pointer, the function it pointers to has parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int. And
the function which q points to has a return type : pointer(this pointer also points to a function which has no parameter and the function's return type is int).

Related

Are these lines of code in C programming the same

Are these 2 lines of code the same ??
line 1:
void (**foo)(int)
line 2
void *(*foo)(int)
Kindly help me understand on what is happening.
They are not the same.
void (**foo)(int);
foo is a pointer to a pointer to a function that takes an int parameter and returns void.
void *(*foo)(int):
foo is a pointer to a function that takes an int parameter and returns a pointer to void.
Postfix operators like () and [] have higher precedence than unary *, so
T *a[N]; // a is an array of pointer to T
T (*a)[N]; // a is a pointer to an array of T
T *f(); // f is a function returning pointer to T
T (*f)(); // f is a pointer to a function returning T

What does an asterisk in empty parentheses mean? [duplicate]

This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 6 years ago.
What does this c code do?
{
int (*func)();
func = (int (*)()) code;
(int)(*func)();
}
Especially I confused about subj.
It's a cast to a function pointer.
The sequence int (*)() is for a function pointer that takes an indeterminate number of arguments, and returns an int. Wrapping it in parentheses like (int (*)()), when combined with an expression, is casting the result of the expression.
The code you present, with comments:
// Declare a variable `func` which is a pointer to a function
int (*func)();
// Cast the result of the expression `code` and assign it to the variable `func`
func = (int (*)()) code;
// Use the variable `func` to call the code, cast the result to `int` (redundant)
// The returned value is also discarded
(int)(*func)();
Remember to do a typecast, we use the following:
(type_to_cast) value;
when you want to cast some value to a certain type.
Also remember you define a function pointer as
return_type (*pointer_name) (data_types_of_parameters);
And the type of a function pointer is
return_type (*) (data_types_of_parameters)
Finally, you can call a function with its pointer as
(*func_pointer)(arguments);
So, with those 4 points in mind, you see that your C code:
First defines a funciton pointer func.
Second, casts code as a function pointer and assign its value to func
Third, calls the function pointed by func, and casts the value reutrned to int.
int (*func)(); declares func as a pointer to a function that takes any number of parameters and and return int.
In statement func = (int (*)()) code;, a cast is applied to code and then assign it to the function pointer func.
(int)(*func)(); doesn't make much sense. Cast is not needed and it discards the return value. The call should be simply like
int var = func();
or
int var = (*func)();

Basic C Pointer and Functions

Im trying to figure out what the function declarations mean:
int *f();
and
int (*g)();
int *f();
The above line is the declaration of a function f that has an unspecified number of parameters and that returns an int *.
int (*g)();
The above line is the declaration of a pointer g to a function that has an unspecified number of parameters and that returns an int.
As an addition to the other correct answers here, I thought I'd mention that cdecl(1) is a handy tool for deciphering these sorts of declarations:
$ cdecl
Type `help' or `?' for help
cdecl> explain int *f();
declare f as function returning pointer to int
cdecl> explain int (*g)();
declare g as pointer to function returning int
cdecl may already be installed on your machine, or you can use it via a handy web interface at http://cdecl.org.
f is a function, returning int* and g is a pointer to function returning int.
Postfix operators such as function-call () have higher precedence than unary operators like *, so
T *f();
reads as
f -- f
f() -- is a function (() binds before *)
*f() -- returning a pointer
T *f(); -- to T (for some type T)
If you want to force * to bind before (), you must explicitly group with parentheses, so
T (*g)();
reads as
g -- g
(*g) -- is a pointer
(*g)() -- to a function
T (*g)(); -- returning T.
The rule is similar for arrays: T *a[N] is an array of pointer to T, whereas T (*p)[N] is a pointer to an array of T.

What's the meaning of this declaration?

I saw this one in one of the .h files provided by my professor for a project, but I'm not too sure what this means.
int (*get_console_dev)(void);
Is it the same as
(int*) get_console_dev(void);
(a function named get_console_dev, which returns a pointer to an int?)
Thanks
It's a function pointer that can point to a function returning int and taking 0 parameters; it's not equivalent to the second line you posted.
int (*get_console_dev)(void) is a function pointer declaration. get_console_dev is a variable that can hold a reference to a function matching the specific signature.
int consoleFunction(void);
// ...
int (*get_console_dev)(void);
get_console_dev = consoleFunction;
This declaration is not the same as: (int*) get_console_dev(void);.
Check this with cdecl.
int (*get_console_dev)(void);
This is Pointer pointer function with no parameter and return type as integer.
(int*) get_console_dev(void);
This is function with no parameter and return type as pointer of type integer.
Both are completely different.
Please read up function pointers. it is a pointer to a function that takes void (as argument) and returns integer

What does double* (*p[3]) (void* (*)()); mean?

I am having difficulty trying to understand what the following declaration means. Is this declaration standard?
double* (*p[3]) (void* (*)());
Can anyone help me to understand the meaning of this declaration?
Rule for reading hairy declarations: find the leftmost identifier and work outward, remembering that () and [] bind before *, so T *a[N] is an array of pointers to T, T (*a)[N] is a pointer to an array of T, T *f() is a function returning a pointer to T, and T (*f)() is a pointer to a function returning T. Since a function prototype may omit parameter names, you may see things like T *[N] or T (*)(). The meaning is mostly the same1, just pretend that there's an identifier of 0 length.
Thus,
p -- p
p[3] -- is a 3-element array
*p[3] -- of pointers
(*p[3]) ( ) -- to functions
(*p[3]) ( (*)()) -- taking a pointer to a function
(*p[3]) ( * (*)()) -- returning a pointer
(*p[3]) (void* (*)()) -- to void
* (*p[3]) (void* (*)()) -- returning a pointer
double* (*p[3]) (void* (*)()); -- to double
The important thing to take away here is that you are declaring p as an array of ..., not a function returning ....
What would such a beast look like in practice? Well, first, you need three functions to point to. Each of these functions takes a single parameter, which is a pointer to a function returning a pointer to void:
double *foo(void *(*)());
double *bar(void *(*)());
double *bletch(void *(*)());
double *(*p[3]) (void *(*)()) = {foo, bar, bletch};
Each of foo, bar, and bletch would call the function passed to it and somehow return a pointer to double.
You would also want to define one or more functions that satisfy the parameter type for each of foo, bar, and bletch:
void *blurga() {...}
so if you called foo directly, you'd call it like
double *pv;
...
pv = foo(blurga);
So we could imagine a call like
double *pv = (*p[0])(blurga);
1 - the difference is that in the context of a function parameter declaration, T a[] and T a[N] are identical to T *a; in all three cases, a is a pointer to T, not an array of T. Note that this is only true in a function parameter declaration. Thus, T *[] will be identical to T **.
Just use http://cdecl.org:
declare p as array 3 of pointer to function (pointer to function returning pointer to void) returning pointer to double
For more info, see this MSDN article: Interpreting more complex declarators.
But typedefs would help:
typedef void *(*foo)(); // foo is a function-pointer type
typedef double *(*bar)(foo); // bar is also a function-pointer type
bar p[3];
(Obviously, use appropriate names in place of foo and bar!)
Your p is an array of 3 pointers to a function returning a double pointer, and taking as argument a pointer to another function that returns a void pointer and that takes no arguments.
But, don't use this syntax, try using typedef instead.
It is array (of size 3) of function pointers which returns pointer to double and take another function pointer as argument.
Type of function whose pointer can be stored in the array: double *(func)(void* (*)())
Type of function whose pointer can be passed as argument to func: void *(func1)(void)
"There is a technique known as the ``Clockwise/Spiral Rule'' which enables any C programmer to parse in their head any C declaration!"
Clockwise Spiral Rule - http://c-faq.com/decl/spiral.anderson.html

Resources