你真的瞭解C中的聲明嗎 —— 簡版dcl程式

來源:互聯網
上載者:User

標籤:

小測試看看你理解對幾個:char **argv    argv: pointer to char array  int (*daytab)[13]    daytab: pointer to array[13] of intint *daytab[13]    daytab: array[13] of pointer to intvoid *comp()    comp: function returning pointer to voidvoid (*comp)()    comp: pointer to function returning voidchar (*(*x())[])()    x: function returning pointer to array[] of    pointer to function returning charchar (*(*x[3])())[5]    x: array[3] of pointer to function returning    pointer to array[5] of char-----------------------------------------------------------------------------------------------------------------------------------/* 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");        }} #include <stdio.h>#include <string.h>#include <ctype.h>#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;} 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;}

 

你真的瞭解C中的聲明嗎 —— 簡版dcl程式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.