The C programing language chapter 6 : struct

来源:互联网 发布:mac网页视频怎么全屏 编辑:程序博客网 时间:2024/05/17 01:53

the chap6 struct

1. basics of structures

//1. define a struct way 1:struct point {    int x;    int y;};way 2:struct point {    int x;    int y;}p_test, *p_pointer;way 3:struct {    int x;    int y;}point; //this point just only a variale such as a from statement : int a; /*this define a struct point that is similar  type int declare a struct point variable such as :*///2. declare a structstruct point p1, p2; //just like declare a normal variable except add precedence  struct //3. initialize struct variable p1way 1 : p1.x = 1;        p1.y = 2;way 2 : define and initializestruct point p1 = {1, 2};//4. struct of pointer,and handle it's members(x, y)struct point *p;p = &p1;p ->x = 4;p ->y = 5;

2. structures and functions

//1. return a pointer to struct point struct point makepoint(int x, int y){    struct point temp;    temp.x = x;    temp.y = y;    return temp;}

3. arrays of structures

//declare array of struct just like char test[10];struct point pointTable[100]; 

4. pointers to structures

struct point *p_test; //p_test is pointer to the struct point

5. self-referential structures

//define a tree nodestruct tnode {    char *value;    struct tnode *left;    struct tnode *right;}

6. table lookup

struct nlist {    struct nlist *next; //the next pointer to the next ectry    char *name;}struct nlist *nlistTab[100];//just like pointer array

7. typedef

//typedef for creating new data type names. for example ,the declarationtypedef int _int;typedef char *string;typedef struct tnode *tnode_pointer;typedef struct tnode {    char *value;    tnode_pointer left;    tnode_pointer right;}Treenode; //creating two type : tnode_pointer and Treenode

8. unions

union {    int ival;    float fval;    char *sval;} u;union u_tay {    int ival;    float fval;    char *sval;};union u_tay {    int ival;    float fval;    char *sval;} u;

9. bit-fields

struct {    unsigned int is_keyword : 1; //is_keyword width is 1 that maining the it's size is 1 bit    unsigned int is_extern : 1;    unsigned int is_static : 1;} flags;flags.is_keyword = 1;
#ifndef STDFSH#define STDFSH 1#include "stdfs.h"#endif/*有一点我就很疑惑,对注释和字符串的处理,是保存还是忽略不管呢因为上一个getword是忽略了\/**\/ 和 “” 这个些符号之后会继续处理他们里面的内容,所以我这里就是忽略到他们里面的内容,其余的几个课后题我发现理解不了,然后去看答案,发现和题目也不是很对照啊,所以就留着以后有空在解决把,就把书上的tree的搞一个就可以了。getword : get next word or character from input*/extern int getch();extern void ungetch(int c);int getword(char *word, int lim){    int c, temp;    char *w = word;    while (isspace(c = getch()))        ;    if (c != EOF)        *w++ = c;    if (isalpha(c) || c == '_' || c == '#')        for ( ;--lim > 0; ) {            *w++ = c = getch();            if (!isalnum(c) && c != '_') {               ungetch(c);                break;            }       }    else if (c == '\'' || c == '"') {         for ( ;temp = getch() ; )            if (temp == c || temp == EOF)                 break;        }    else if (c == '/') {                    if ((temp = getch()) == '*') {            for ( ;temp = getch(); )                if (temp == EOF || temp == '*' && getch() == c)                    break;        } else             ungetch(temp);    }     *--w = '\0';    if (temp == EOF)        return EOF;    return c;    }int comment(){    int c;    while (c=getch() != EOF)        if (c == '*')            if ((c = getch()) == '/')               break;            else                ungetch(c);    return c;}
0 0
原创粉丝点击