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
Related
Is the code I have written valid? I am trying to write an expression which is a pointer to an array of pointers to functions which takes a single argument int and return void. So function has return type void. But I am not sure if my expression of (**[]) is valid or should I write it as (*(*[]))? Because the later means array of pointers to a function which is a pointer to a function.
void(*(*[])(int));
Do it like this
typedef void (*fptr)(int)
fptr array[10];
it's clearer than trying to roll it all into one bit of syntax.
I have been attempting C recently and have been reading C source code,
however I came across this perculiar method signature with a * beside its name,
can someone explain this to me please
int *bubble_sort(int *numbers, int count, compare_cb cmp)
edit: I am referring to the * infront of bubble_sort
The asterisk has nothing to do with the function signature, it's just C's way of declaring pointers. The asterisk belongs to the type on the left, i.e. the return type for the function is int *, which is read out as "pointer to integer". The first argument has the exact same type, pointer to integer. You use this syntax everywhere in C, not just with function declarations.
The '*', used in this way, indicates that the 'bubble_sort()' function returns an address of (in other words, 'a pointer to) an integer, rather than an integer.
This concept is of pointers.
To know more about pointers see this link : http://www.tutorialspoint.com/cprogramming/c_pointers.htm
int *bubble_sort(int *numbers, int count, compare_cb cmp)
int *bubble_sort means that you are going to return the address of the integer,
and int *numbers means that you are getting the address of the variable as argument.
int a() is a function returning an int.
int * a() is a function returning a pointer to an int.
in your case the function returns a pointer to an integer.
guessing from the function name it is returning the sorted array.
an array in C is notated as a pointer to the first element:
int a[5];
int *b=a;
a and b are the same
However I guess you still have to do some more research on pointers which is not as complicated.
Perhaps this links might help: Pointer Primer
In C what's the function pointer (void*) doing in:
int (*fn) (void*)
If the parameter is nothing then it should be:
int (*fn) ()
My understanding is void* is chunk of memory. void* mem means mem pointing to a chunk of memory. But what's (void*) without a name?
That function pointer declaration does not require you to give the void* a name. It only requires a type to define the argument list.
This is similar to how:
void my_function(int x);
is just as valid as
void my_function(int);
void * is an anonymous pointer. It specifies a pointer parameter without indicating the specific data type being pointed to.
An unnamed parameter is not the same thing as no parameters. The declaration:
int (*fn)(void*);
merely states that fn is a function pointer that takes a void* argument. The parameter name is inconsequential (and is only meaningful in the function implementation where it's the name of the local variable).
(Although it the parameter name is not necessary in function declarations, it is nevertheless useful to people reading the code to identify what the parameter is.)
It means that fn is "any function that takes a block of memory as a parameter, and returns an int"
I have a method I need to fill in and one of the parameters is: "void (*destructor)(void*)". Can anyone tell me what this is?
It is a Function pointer.
It stores address of an function which takes a void *(pointer to void) as an input parameter and returns no parameter.
That's a function pointer. The function given should have the signature:
void funcname(void*)
The first void is the return type, the second is a pointer to void (in other words, a pointer to who knows what, which the underlying function will presumably cast to some useful type later).
The void (*destructor)(void*) is a function pointer. This means, that you can use it to point to any function, which has void as return value (returns nothing) and expects an generic pointer (void*), which can point to anything.
It's pointer to a function that takes a void* as its only argument, and returns nothing.
What does this do?
int *(*pfp) ();
int *(*pfp) ();
Creates a pointer to a function that returns int*. The function pointer name ispfp.
Here's an example:
int* myFunc(void)
{
return NULL;
}
int main(int argc, char**argv)
{
int *(*pfp) (void);
pfp = myFunc;
return 0;
}
Note: Since the parameters of the function pointer is not (void) that you gave, that means that the parameter list is not specified in C. In C++ it would mean that it's a function with no parameters exactly.
Brian's explained the "what", but if you're curious as to the why, here's an excerpt from Dennis Ritchie's The Development of the C Language:
The second innovation that most clearly distinguishes C from its predecessors is this fuller type structure and especially its expression in the syntax of declarations. NB offered the basic types int and char, together with arrays of them, and pointers to them, but no further ways of composition. Generalization was required: given an object of any type, it should be possible to describe a new object that gathers several into an array, yields it from a function, or is a pointer to it.
For each object of such a composed type, there was already a way to mention the underlying object: index the array, call the function, use the indirection operator on the pointer. Analogical reasoning led to a declaration syntax for names mirroring that of the expression syntax in which the names typically appear. Thus,
int i, *pi, **ppi;
declare an integer, a pointer to an integer, a pointer to a pointer to an integer. The syntax of these declarations reflects the observation that i, *pi, and **ppi all yield an int type when used in an expression. Similarly,
int f(), *f(), (*f)();
declare a function returning an integer, a function returning a pointer to an integer, a pointer to a function returning an integer;
int *api[10], (*pai)[10];
declare an array of pointers to integers, and a pointer to an array of integers. In all these cases the declaration of a variable resembles its usage in an expression whose type is the one named at the head of the declaration.
For more info, read Steve Friedl's "Reading C type declarations" or Eric Giguere's "Reading C Declarations: A Guide for the Mystified".
pfp is a pointer to a function that takes no parameters (or indeterminate parameters in C) and returns a pointer to an int.
Using the 'clockwise-spiral' rule is an effective way to decipher complex C declarations (at least I find it effective):
http://c-faq.com/decl/spiral.anderson.html
Use http://www.cdecl.org/ for these things, if you're not sure.