How do I read this declaration?
I'm trying to apply operator precedence to unravel it, but the outer void and (int) are really throwing me off.Code:void (*signal(int sig, void (*handler)(int)))(int)
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 ...
How do I read this declaration?
I'm trying to apply operator precedence to unravel it, but the outer void and (int) are really throwing me off.Code:void (*signal(int sig, void (*handler)(int)))(int)
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.)
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);
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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:
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.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("%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; }![]()
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.