[SOLVED] I need help understanding this declaration in C...
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You haven't seen pointers to functions before? I can't say I really know what they're for, as I've never used them myself . Hopefully someone else will provide a better explanation!
Perfect, I will read up, and then come back and hopefully be able to explain what's going on above as so as to help others that come across my post. Thanks again.
defines something as a pointer to a function, which returns nothing (void), but takes one integer as a parameter (int).
The way you use function pointers is simple; you just assign the pointer, then use it as if it were a function. Consider this example:
Code:
#include <stdio.h>
int (*operation)(int, int);
int addition(int a, int b) { return a + b; }
int substraction(int a, int b) { return a - b; }
int main(void)
{
printf("operation = addition;\n");
operation = addition;
printf("/* operation(1, 2) == %d */\n", operation(1, 2));
printf("\n");
printf("operation = substraction;\n");
operation = substraction;
printf("/* operation(1, 2) == %d */\n", operation(1, 2));
return 0;
}
You can, but do not need to use the & operator in front of a function name, as long as you omit the parentheses (). The compiler knows you mean the address of the function in any case. I prefer to not use it, so the compiler will warn me if the target is not a function (or a function pointer). Also, you cannot use the & when the target is a function pointer (because then you'd take the address of the pointer, not the target of the pointer). So I generally recommend to not use the & operator when taking the address of a function.
defines three function pointers as fields in the structure:
sa_handler is a pointer to a function which takes one int as a parameter, and returns nothing (void).
sa_sigaction is a pointer to a function which takes three parameters: an int, a pointer to a siginfo_t (which happens to be a structure), and a void pointer.
sa_restorer is a pointer to a function which takes no parameters (void), and returns nothing (void).
The sigaction() function call understands two constants for sa_handler: SIG_IGN and SIG_DFL . They are both pointers to a function that takes one int as a parameter, and returns nothing.
Note that SIG_DFL and SIG_IGN are just pointers, not real functions. For a number of reasons, they are defined to point to specific addresses (in Linux, to addresses 0 and 1, respectively); just like NULL points to address 0. So, while you could call SIG_IGN(SIGUSR1) or SIG_DFL(SIGHUP), doing that will crash your program since SIG_IGN and SIG_DFL do not point to any real functions.
Hope this clarifies the issue for you.
Last edited by Nominal Animal; 07-24-2011 at 04:54 PM.
Reason: added the note about & with function pointers.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.