LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2011, 01:10 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
I need help understanding this declaration in C...


Code:
           struct sigaction {
               void     (*sa_handler)(int);
               void     (*sa_sigaction)(int, siginfo_t *, void *);
               sigset_t   sa_mask;
               int        sa_flags;
               void     (*sa_restorer)(void);
           };
On the 2nd line
Code:
void (*sa_handler)(int);
Is the line declaring a void pointer with size of int?
 
Old 07-24-2011, 01:49 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
No. It's a pointer to a function that returns void and takes an int as an argument. For example, if you had a function

void foo(int x) { // Do stuff },

you could write

struct sigaction sa;
sa.sa_handler = &foo;,

where & is the address of operator.

Last edited by Nylex; 07-24-2011 at 02:03 PM.
 
Old 07-24-2011, 02:45 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
It's a pointer to which function?
 
Old 07-24-2011, 02:46 PM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Any function that returns void and takes an int as an argument. The pointer hasn't been given an address yet, so it's currently null.

Edit: In the example I gave above, sa_handler would be a pointer to the function foo().
 
Old 07-24-2011, 03:19 PM   #5
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
Interesting, never seen it done this way, but ok. Thanks for explaining Nylex.
 
Old 07-24-2011, 03:22 PM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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!
 
Old 07-24-2011, 03:31 PM   #7
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
So in a case like this.
Code:
        // ignore SIGPIPE
        struct sigatction act;
        sigaction(SIGPIPE, (struct sigaction *)NULL, &act);  // grabs old action
        if(act.sa_handler == SIG_DFL)           // if default
        {
                act.sa_handler == SIG_IGN;   // sets handler to ignore
                sigaction(SIGPIPE, &act, (struct sigaction *)NULL); // puts in new action
        }
Code:
act.sa_handler == SIG_IGN;    // which sets it to ignore
SIG_IGN is also an int.
Trying to tie in
Code:
void (*sa_handler)(int)
in this situation. How does this apply?

Last edited by trist007; 07-24-2011 at 03:34 PM.
 
Old 07-24-2011, 04:26 PM   #8
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello,

you may take a look at the "function-pointer tutorial" http://www.newty.de/fpt/index.html
it has some useful examples.

Btw, I've always found this pointers to be an interesting concept, but I'm not really a C-programmer and so I never have used it.

Markus
 
Old 07-24-2011, 04:44 PM   #9
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
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.
 
Old 07-24-2011, 04:47 PM   #10
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Code:
void (*something)(int);
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.

When compiled and run, the program will output
Code:
operation = addition;
/* operation(1, 2) == 3 */

operation = substraction;
/* operation(1, 2) == -1 */
When defined in a structure, function pointers behave exactly the same. For example,
Code:
           struct sigaction {
               void     (*sa_handler)(int);
               void     (*sa_sigaction)(int, siginfo_t *, void *);
               sigset_t   sa_mask;
               int        sa_flags;
               void     (*sa_restorer)(void);
           };
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.
 
1 members found this post helpful.
Old 07-24-2011, 07:43 PM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
There are many peculiar things that you will find in "C" code. It is a language designed to be expressive of very low-level constructs when necessary.
 
Old 07-25-2011, 08:46 AM   #12
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
Thanks a lot that cleared it up.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] QWebView, forward declaration, "error: forward declaration of 'struct QWebView'" Squall90 Programming 1 12-28-2010 05:40 AM
declaration of independence carlos marlos Linux - General 2 05-09-2006 12:44 AM
char * declaration in c; alaios Programming 19 09-14-2005 09:50 PM
the c declaration kapsikum Programming 3 04-06-2005 02:12 AM
How to understand the declaration? Xiangbuilder Programming 8 09-13-2003 04:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration