Thread: C gurus, how to understand these expressions?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    64

    C gurus, how to understand these expressions?

    Code:
           if ((fp->flag&(_READ|_EOF_ERR)) != _READ)
               return EOF;
    Code:
           { 0, (char *) 0, (char *) 0, _WRITE, | _UNBUF, 2 }
    plz pay attention to the part bolded and colored.
    Last edited by thinhare; 01-11-2005 at 05:56 AM.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    The above is excerpted from the below.
    Hope this helps.
    Code:
    Once the buffer is established, _fillbuf calls read to fill it, 
    sets the count and pointers, and returns the character
    at the beginning of the buffer. Subsequent calls to _fillbuf
    will find a buffer allocated.
    
       #include "syscalls.h"
    
       /* _fillbuf:  allocate and fill input buffer */
       int _fillbuf(FILE *fp)
       {
           int bufsize;
    
           if ((fp->flag&(_READ|_EOF_ERR)) != _READ)
               return EOF;
           bufsize = (fp->flag & _UNBUF) ? 1 : BUFSIZ;
           if (fp->base == NULL)     /* no buffer yet */
               if ((fp->base = (char *) malloc(bufsize)) == NULL)
                   return EOF;       /* can't get buffer */
           fp->ptr = fp->base;
           fp->cnt = read(fp->fd, fp->ptr, bufsize);
           if (--fp->cnt < 0) {
               if (fp->cnt == -1)
                   fp->flag |= _EOF;
               else
                   fp->flag |= _ERR;
               fp->cnt = 0;
               return EOF;
           }
           return (unsigned char) *fp->ptr++;
       }
    
    The only remaining loose end is how everything gets
    started. The array _iob must be defined and initialized
    for stdin, stdout and stderr:
    
       FILE _iob[OPEN_MAX] = {    /* stdin, stdout, stderr */
           { 0, (char *) 0, (char *) 0, _READ, 0 },
           { 0, (char *) 0, (char *) 0, _WRITE, 1 },
           { 0, (char *) 0, (char *) 0, _WRITE, | _UNBUF, 2 }
       };

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I believe the second bolded spot you have is a typo. You seem to have added a comma after _WRITE, which shouldn't be there. Try and compile that and you'll find out what I mean.

    What you're seeing here is the bitwise OR operator. For a full explanation, consider reading this FAQ entry. Basicly it's OR-ing two values on a bit by bit basis, giving you a resulting value which has all set bits of both values combine.

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

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    basically _WRITE and _UNBUF are, more than likely, macros #defined somewhere. whatever their values are, they are bitwise OR'd together; _WRITE | _UNBUF.

    if _WRITE = 00000001
    and _UNBUF = 00000010
    then (to my knowledge) ORing them will result
    00000011

    Read up on bitwise operations.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    thanks for replys from you both.

    I know some about bitwise, and here what that confuses me is whether a "|" has been missed by typo.

    and yes, they've been defined before being used.
    Code:
       enum _flags {
           _READ   = 01,   /* file open for reading */
           _WRITE  = 02,   /* file open for writing */
           _UNBUF  = 04,   /* file is unbuffered */
           _EOF    = 010,  /* EOF has occurred on this file */
           _ERR    = 020   /* error occurred on this file */
       };
    so I'm guessing:
    Code:
    if ((fp->flag&(_READ|_EOF_ERR)) != _READ)
    is:
    Code:
    if ((fp->flag&(_READ|_EOF|_ERR)) != _READ)
    and:
    Code:
    { 0, (char *) 0, (char *) 0, _WRITE, | _UNBUF, 2 }
    is:
    Code:
    { 0, (char *) 0, (char *) 0, _WRITE | _UNBUF, 2 }
    Do you agree? Or you have the same book, K&R - The C programming language, please refers to chapter 8.5-Examples to see whether they are same as that in my book.

    ...
    Last edited by thinhare; 01-11-2005 at 07:42 AM.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    Im trying to figure out what they exactly mean.

    1) (_READ|_EOF|_ERR) set corresponding bits to 1;
    fp->flag AND with it, to see whether result is _READ, namely _EOF and _ERR are gone.

    2) like what quazh and sand_man said.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    sigh! so hard!

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    They look like typos to me but you can only guess.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    if it compiles then i would assume _EOF_ERR == 0011
    given _EOF = 0010 //made up value
    and _ERR = 0001 //made up value

    i don;t know where _EOF_ERR would be defined though
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Program to evaluate expressions
    By DMEngOba in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2009, 09:16 PM
  2. Stack that evaluates postfix expressions
    By killmequick in forum C Programming
    Replies: 7
    Last Post: 10-01-2008, 06:23 PM
  3. Evaluationof expressions
    By plan7 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 07:18 AM
  4. Regular expressions [Boost]
    By Desolation in forum C++ Programming
    Replies: 8
    Last Post: 12-30-2006, 10:10 PM
  5. Prefix Expressions
    By mannyginda2g in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2004, 01:30 AM