Thread: Accessing Member from Structure Array

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    Accessing Member from Structure Array

    Code:
    /* Exercise 8-2. Rewrite fopen and _fillbuf with fields instead of explicit bit operations.
    Compare code size and execution speed.
    */
    
    
    /* exercise.8-2.h */
    #define NULL      0
    #define EOF       (-1)
    #define BUFSIZ    1024
    #define OPEN_MAX  20    /* max #files open at once */
    
    struct _flags    /* bit-field vs define bitmasks */
    {
        unsigned int _READ   : 1;
        unsigned int _WRITE  : 1;
        unsigned int _UNBUF  : 1;
        unsigned int _EOF    : 1;
        unsigned int _ERR    : 1;
    };
    
    typedef struct _iobuf
    {
        int cnt;                /* characters left */
        char *ptr;              /* next character position */
        char *base;             /* location of buffer */
        struct _flags flag;        /* mode of file access */
        int fd;                 /* file descriptor */
    } FILE_;
    
    FILE_ _iob[OPEN_MAX] = {    /* stdin, stdout, stderr */
                                { 0, (char *) 0, (char *) 0, 0, 0 },
                                { 0, (char *) 0, (char *) 0, 0, 1 },
                                { 0, (char *) 0, (char *) 0, 0, 2 }
                            };
    
    _iob[0].flag._READ = 1;
    _iob[0].flag._WRITE = 0;
    _iob[0].flag._UNBUF = 0;
    _iob[0].flag._EOF = 0;
    _iob[0].flag._ERR = 0;
    _iob[1].flag._READ = 0;
    _iob[1].flag._WRITE = 1;
    _iob[1].flag._UNBUF = 0;
    _iob[1].flag._EOF = 0;
    _iob[1].flag._ERR = 0;
    _iob[2].flag._READ = 0;
    _iob[2].flag._WRITE = 1;
    _iob[2].flag._UNBUF = 1;
    _iob[2].flag._EOF = 0;
    _iob[2].flag._ERR = 0;
    In file included from exercise.8-2.c:3:
    exercise.8-2.h:30: warning: large integer implicitly truncated to unsigned type
    exercise.8-2.h:33: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:34: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:36: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:37: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:38: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:39: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:40: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:41: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:42: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:43: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:44: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:45: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:46: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    exercise.8-2.h:47: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    I can't figure why I'm getting these errors. I'd really appreciate some help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't just scatter variable assignments in open space there. They need to be inside a function call.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    Thank You.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could have also done this:
    Code:
    FILE_ _iob[OPEN_MAX] = {    /* stdin, stdout, stderr */
                                { 0, (char *) 0, (char *) 0, { 1,0,0,0,0, }, 0 },
                                { 0, (char *) 0, (char *) 0, { 0,1,0,0,0, }, 1 },
                                { 0, (char *) 0, (char *) 0, { 0,1,1,0,0, }, 2 }
                            };
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    I was wondering about that. I was just about to declare my bit field as a union with an integer in it so I could assign a value to it. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incrementing Structure Member Array
    By rrc55 in forum C Programming
    Replies: 5
    Last Post: 09-22-2009, 12:45 PM
  2. Problem defining structure
    By MTK in forum C Programming
    Replies: 12
    Last Post: 09-08-2009, 03:26 PM
  3. Assignment to a structure array member
    By sj999 in forum C Programming
    Replies: 4
    Last Post: 05-22-2008, 11:13 PM
  4. error: ‘struct tcphdr’ has no member named ‘th_win’
    By nasim751 in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 12:07 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM