Use struct to create a boolean type

This is a discussion on Use struct to create a boolean type within the C Programming forums, part of the General Programming Boards category; Since C doesn't have boolean type, I'm trying to use typedef to create a boolean data type. How do I ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    28

    Use struct to create a boolean type

    Since C doesn't have boolean type, I'm trying to use typedef to create a boolean data type. How do I use structure to create such boolean type (I saw somewhere, but I forgot) ?

    Code:
    typdef int boolean
    #define TRUE 1
    #define FALSE 0
    This code works, but it doesn't use struct, anyone can help me implement a struct datatype of boolean? Thanks

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    You probably mean an enumerated type rather than a structure...
    Code:
    typedef enum {false, true} boolean;
    DavT
    -----------------------------------------------

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    union mybool {
        int true;
        int false;
    };
    There. You now have a variable which can represent true and false, but only one at once, never both at the same time.

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

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    28
    thanks, enumerated type is what I was looking for..
    and by the way, I haven't learned union, how do I use it?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by skyglin
    thanks, enumerated type is what I was looking for..
    and by the way, I haven't learned union, how do I use it?
    The code for the union is actualy a subtle joke. However, what a union does is makes a data type whose members share the same physical memory space.

    For example, if you needed (typically) 4 bytes to be used either for a floating point number, or an integer, you may be inclined to do:
    Code:
    union foo
    {
        float f;
        long l;
    };
    
    struct bar
    {
        char type;
        union foo data;
    };
    
    ... stuff ...
    
    struct bar foobar;
    
    if( foobar.type == SOMETHING )
    {
         foobar.f = 10.3;
    }
    else
    {
        foobar.l = 10;
    }
    Crude example, but sufficient. The thing about unions is that you use either f or l, but not both. At least not at the same time. They both take the same physical memory address.

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

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    28
    >>>what a union does is makes a data type whose members share the same physical memory space.

    How does the compiler handle the memory space if the memebers of a union occupy memory in different sizes, for example, a union of a char(1 byte) and an int(4 bytes).

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <stdio.h>
    
    union foo 
    {
      unsigned int i;
      unsigned char c;
    };
    
    int main( ) 
    {
      union foo a;
      a.i = 0x12345678;
      printf( "sizeof( a ) = %d\na.i = 0x%X\na.c = 0x%X", sizeof( a ), a.i, a.c );
      return 0;
    }
    /*
    OUTPUT:
    sizeof( a ) = 4
    a.i = 0x12345678
    a.c = 0x12 OR 0x78, depending on system
    */
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21