Thread: need help with structures

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    need help with structures

    Code:
    struct cat 
    {
           
           int bHungy = 0; /* "b" indicates boolean */
           int bNeedto........ = 0;
           int bNeedto$$$$ = 0;
           int bWantpets = 0;
           int bWantfood = 0;
    } sugar;
    I keep getting errors, yet I'm following the style of my book!

  2. #2
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    the $$$ is a censor that the board put up. In a less offensive way, teh $$$ stands for crap.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Alright... that makes sense... what errors are you getting?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    i found my error. Appearantly, I can't define a variable while I'm also defining a structure. This code works:

    Code:
    struct cat 
    {
           int bHungy;
           int bNeedto........;
           int bNeedtocrap;
           int bWantpets;
           int bWantfood;
    } sugar;
    I do have one other question: what headers do I need to make a bool variable?

  5. #5
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    What? Why not have a boolean? It would be the smallest variable type - 1 bit. I would think that some things that programmers would want todo in C would only rely on true or false.

    I saw that GCC supports some C99, but not all. Does any compiler support all?

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by kinghajj
    What? Why not have a boolean? It would be the smallest variable type - 1 bit. I would think that some things that programmers would want todo in C would only rely on true or false.
    Yes it would, but what would you do with the other 7, 15, or 31 bits in the stored location? Bools weren't defines in the original C. Gotta live with it.

    Use a char to cut down the space needed by the program if you think that's important. Or if you have multible bools to define, use each bit in a char to hold each boolean value.

    Define the values as:
    #define MEAT 0x01
    #define MILK 0x02
    #define VEGE 0x04
    char boolValues = 0;

    Set meat bit with
    boolValues |= MEAT;

    Clear milk bit with
    boolValues &= ~MILK;

    You can figure out how to test the value....
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM