Question about a declaration

This is a discussion on Question about a declaration within the C Programming forums, part of the General Programming Boards category; How do I read this declaration? Code: void (*signal(int sig, void (*handler)(int)))(int) I'm trying to apply operator precedence to unravel ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    54

    Question about a declaration

    How do I read this declaration?

    Code:
    void (*signal(int sig, void (*handler)(int)))(int)
    I'm trying to apply operator precedence to unravel it, but the outer void and (int) are really throwing me off.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    sig is an int.
    handler is a pointer to a function that takes an int and returns a void.
    signal is a function that takes sig and handler as parameters, and returns a pointer. That pointer, when dereferenced, is a function takes an (unnamed) int parameter and returns void. (I think. I may have gotten a pointer and a function turned around.)

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Here's a breakdown of the function, much easier to see and understand because someone neglected to create readable code ():

    typedef void (handler_t)(int);
    typedef void (signal_return_t)(int);
    signal_return_t* signal(int sig, handler_t* handler);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    There's actually a program in The C Programming Language that splits declarations like this . . . it starts on page 90 of this: http://www.ica.luz.ve/dfinol/tpro/kandr.pdf

    Since that's probably an illegal copy of the book, I've copied the code here for you:
    Code:
    /* From The C Programming Language */
    
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define BUFSIZE 100
    char buf[BUFSIZE];     /* buffer for ungetch */
    int bufp = 0;          /* next free position in buf */
    int getch(void) /* get a (possibly pushed-back) character */
    {
        return (bufp > 0) ? buf[--bufp] : getchar();
    }
    void ungetch(int c)    /* push character back on input */
    {
        if (bufp >= BUFSIZE)
             printf("ungetch: too many characters\n");
        else
             buf[bufp++] = c;
    }
    
    #define MAXTOKEN   100
    enum { NAME, PARENS, BRACKETS };
    void dcl(void);
    void dirdcl(void);
    int gettoken(void);
    int tokentype;             /* type of last token */
    char token[MAXTOKEN];      /* last token string */
    char name[MAXTOKEN];       /* identifier name */
    char datatype[MAXTOKEN];   /* data type = char, int, etc. */
    char out[1000];
    main() /* convert declaration to words */
    {
        while (gettoken() != EOF) {     /* 1st token on line */
            strcpy(datatype, token); /* is the datatype */
            out[0] = '\0';
            dcl();         /* parse rest of line */
            if (tokentype != '\n')
                printf("syntax error\n");
            printf("&#37;s: %s %s\n", name, out, datatype);
        }
        return 0;
    }
    
    /* dcl: parse a declarator */
    void dcl(void)
    {
        int ns;
        for (ns = 0; gettoken() == '*'; ) /* count *'s */
             ns++;
        dirdcl();
        while (ns-- > 0)
             strcat(out, " pointer to");
    }
    /* dirdcl: parse a direct declarator */
    void dirdcl(void)
    {
        int type;
        if (tokentype == '(') {          /* ( dcl ) */
             dcl();
             if (tokentype != ')')
                 printf("error: missing )\n");
        } else if (tokentype == NAME) /* variable name */
             strcpy(name, token);
        else
             printf("error: expected name or (dcl)\n");
        while ((type=gettoken()) == PARENS || type == BRACKETS)
      if (type == PARENS)
          strcat(out, " function returning");
      else {
          strcat(out, " array");
          strcat(out, token);
          strcat(out, " of");
      }
    }
    
    int gettoken(void) /* return next token */
    {
        int c, getch(void);
        void ungetch(int);
        char *p = token;
        while ((c = getch()) == ' ' || c == '\t')
            ;
        if (c == '(') {
            if ((c = getch()) == ')') {
                strcpy(token, "()");
                return tokentype = PARENS;
            } else {
                ungetch(c);
                return tokentype = '(';
            }
        } else if (c == '[') {
            for (*p++ = c; (*p++ = getch()) != ']'; )
                ;
            *p = '\0';
            return tokentype = BRACKETS;
        } else if (isalpha(c)) {
            for (*p++ = c; isalnum(c = getch()); )
                *p++ = c;
          *p = '\0';
          ungetch(c);
          return tokentype = NAME;
      } else
          return tokentype = c;
    }
    It doesn't work as well I remember it doing . . . I must have improved it, but I can't find where I put my version.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to compile projects ?
    By manzoor in forum C++ Programming
    Replies: 31
    Last Post: 10-15-2008, 11:52 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  5. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21