This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 4 years ago.
I am not familiar with the syntax below for the following struct
struct fp {
int (*fp)();
}
What is int (*fp)() ? I understand that it is an integer and *fp is a pointer but I do not understand what that parentheses in (*fp)() do.
fp is a pointer to a function with empty parameters list.
I.e.
int myfunc() //define function
{
return 0;
}
struct fp //define structure
{
int (*fp)();
} mystruct;
mystruct.fp = &myfunc; //assign function pointer to structure element
int a = mystruct.fp(); //Call function through pointer
There are many ways to read C declarations, that can be very complicate in some cases. Start reading this https://parrt.cs.usfca.edu/doc/how-to-read-C-declarations.html.
You can google for "how to read c declarations" for more in depth explanation and further tips.
As Swordfish pointed out, the use of empty parameter list imply other sensible points about function definitions, that could be worth to deep. Please refer to Swordfish's comment below for the main points relative to functions definition.
I will refer only to the §6.11.6 Function declarators (of §6.11 Future language directions):
The use of function declarators with empty parentheses (not
prototype-format parameter type declarators) is an obsolescent
feature.
It is a function pointer. They are powerful and quite hard to get your head around if you are a beginner.
It is a strcture that wraps a function-pointer.
Have a look here: How do function pointers in C work?
It is a declaration of the variable fp which is a pointer to a function that returns an int and takes an unspecified list of arguments.
Related
This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 3 years ago.
I stumbled upon these lines when looking into putchar.c
I'm wondering about why the arguments ptr and c are declared outside the arguments body ?
Is this some kind of "good old way" or does it have some actual use ?
int
_putchar_r (ptr, c)
struct _reent *ptr;
int c;
{
return __sputc (c, _stdout_r (ptr));
}
Indeed it is the "olden" way of declaring a function's parameters.
I kind of like it, because it serves as a constant reminder that all a function's parameters are local variables that exist only in the scope of the function and that any argument passed into any function ALWAYS is a value copy.
Its a K&R C style introduced in classic C Programming Book
It is a function definition with an identifier list. Each identifier in the identifier list is declared before the compound statement of the function.
So a function can be defined either with a parameter type list or using the old style with an identifier list.
I am trying to understand what this means, the code I am looking at has
in .h
typedef void (*MCB)();
static MCB m_process;
in .C
MCB Modes::m_process = NULL;
And sometimes when I do
m_process();
I get segmentations fault, it's probably because the memory was freed, how can I debug when it gets freed?
It defines a pointer-to-function type. The functions return void, and the argument list is unspecified because the question is (currently, but possibly erroneously) tagged C; if it were tagged C++, then the function would take no arguments at all. To make it a function that takes no arguments (in C), you'd use:
typedef void (*MCB)(void);
This is one of the areas where there is a significant difference between C, which does not - yet - require all functions to be prototyped before being defined or used, and C++, which does.
It introduces a function pointer type, pointing to a function returning nothing (void), not taking any parameters and naming the new type MCB.
The typedef defines MCB as the type of a pointer to a function that takes no arguments, and returns void.
Note that MCB Modes::m_process = NULL; is C++, not C. Also, in C, the typedef should really be typedef void (*MCB)(void);.
I'm not sure what you mean by "the memory was freed". You have a static pointer to a function; a function cannot be freed. At most, your pointer has been reset somewhere. Just debug with a memory watch on m_process.
Let's take an example
typedef void (*pt2fn)(int);
Here, we are defining a type pt2fn. Variables of this type point to functions, that take an integer as argument and does not return any value.
pt2fn kk;
Here, kk is a variable of type pt2fn, which can point to any function that takes in an integer as input and does not return any value.
Reference:https://cs.nyu.edu/courses/spring12/CSCI-GA.3033-014/Assignment1/function_pointers.html
It's a function pointer. You get a SEGMENTATION FAULT because you are trying to make a call to a function which address is invalid (NULL).
According to your specific sample, the function should return no value (void) and should receive no parameters ().
This should work:
void a()
{
printf("Hello!");
}
int main(int arcg, char** argv)
{
m_process = a;
m_process(); /* indirect call to "a" function, */
// Hello!
}
Function pointers are commonly used for some form of event handling in C. It's not its only use though...
This question already has answers here:
How do function pointers in C work?
(12 answers)
C function pointer syntax
(4 answers)
Closed 5 years ago.
I've been meaning to ask this question for a while now. What's going on with these functions? Why are the names in parenthesis?
void (*think)(gentity_t *self);
void (*reached)(gentity_t *self); // movers call this when hitting endpoint
void (*blocked)(gentity_t *self, gentity_t *other);
void (*touch)(gentity_t *self, gentity_t *other, trace_t *trace);
In your examples, the parenthesis in function name means that variable of pointing the function address.
If you don't use the parenthesis
void * think(gentity_t *self);// equal (void *) think(gentity_t *self);
It means the definition of a function with name:think, return: void *, parameter: gentity_t *self;
These are the variable of the pointing the functions.
These declarations are function pointers, which point to a function and can be changed at any time.
I suggest you do some research on function pointers in C because they are very useful.
If you know C++'s std::function then these are effectively the old C version of them.
These are function pointers and not the function names. So they can point to any function of same type and properties.
This question already has answers here:
What's the difference between passing by reference vs. passing by value?
(18 answers)
Closed 8 years ago.
What is the difference between passing by reference the parameters in a function and passing pointer variables as a parameter in a function ?
There is no pass by reference in C, it's always pass by value.
C developers can emulate pass by reference, by passing the pointers to a variable and the accessing it using dereferencing within the function. Something like the following, which sets a variable to 42:
static void changeTo42 (int *pXyzzy) {
*pXyzzy = 42;
}
:
int x = 0;
changeTo42 (&x);
Contrast that with the C++ true pass by reference, where you don't have to muck about with pointers (and especially pointers to pointers, where even seasoned coders may still occasionally curse and gnash their teeth):
static void changeTo42 (int &xyzzy) {
xyzzy = 42;
}
:
int x = 0;
changeTo42 (x);
I would implore ISO to consider adding true references to the next C standard. Not necessarily the full capability found in C++, just something that would fix all the problems people have when calling functions.
You might be thinking of C++. I'll cover that below.
In C there is no passing by reference. To accomplish the same feat, you can send a pointer to a variable as an argument and dereference the pointer in the method, as shown in paxdiablo's comment.
In C++, you could accomplish the same thing if you tried to pass by reference C-style (as explained previously) or if you tried passing the arguments as such:
static void multiply(int& x){
x * 7;
}
void main(){
int x = 4;
multiply(x);
}
The variable x at the end of this program would equal 28.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to understand complicated function declarations?
Spiral rule and ‘declaration follows usage’ for parsing C expressions
There is a section with the same title, "Complicated Declarations", in K&R's The C Programming Language book as you might have already read. I am just reading the book and trying to better myself in C language. After reading the section mentioned, I think I couldn't get the logic behind the syntax of C declaration statements. 1, 2, 3 and 4 are from that section 5 and 6 are from other pages.
int (*daytab)[13]
daytab: pointer to array[13] of int
void (*comp)()
comp: pointer to function returning void
char (*(*x())[])()
x: function returning pointer to array[] of pointer to function returning char
char (*(*x[3])())[5]
x: array[3] of pointer to function returning pointer to array[5] of
char
typedef int (*PFI)(char *, char *)
creates the type PFI, for ``pointer to function (of two char *
arguments) returning int. How does the syntax works here?
Finally, my questions are:
Can you explain your ways of thinking and reading complicated
declarations possibly by using examples above?
Are the things like
1,3,4 practically usable and needed? If so, can you write some code examples?
I saw The ``Clockwise/Spiral Rule'' on HackerNews in the past week or so. It is a good way to think about C declarations, especially function pointers.
Look at the identifier and the symbol next to it to the right:
If it's a [ the identifier is for an array.
If it is a ( the identifier is for a function
If it is a ) look to the left and you will find a *: the identifier is a pointer
If there is nothing to the right or to the left, the identifier is a "plain old" object.
Elaboration:
int (*daytab)[13]
daytab is a pointer
void (*comp)()
comp is a pointer
char (*(*x())[])()
x is a function
char (*(*x[3])())[5]
x is an array
typedef int (*PFI)(char *, char *)
PFI is a pointer