Thread: fun with structures

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    fun with structures

    I have two structures:
    Code:
    struct something {
            char id;
            char data[100];
    };
    
    struct somethingelse {
            char name;
            char data[100];
    };
    Without using a union, could I make these two structures as one? I had even thought to make it something like
    Code:
    struct something {
            char id_or_name;
            char data[100];
    };
    but that is so ugly. I haven't tried it (but will), but would this be valid (and the best way to do this):
    Code:
    #define name id
    struct something {
            char id;
            char data[100];
    };
    
    struct something stuff;
    struct something name;
    
    stuff.id = 3;
    name.name = 'j';

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    While using that define is legal, it will probably cause you problems elsewhere in the code (some variable or function will have "name" in it somewhere). It also hurts code readability.

    What is the reasoning for combining the two structures? Is it so you can have one function which takes both types?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by bithub View Post
    What is the reasoning for combining the two structures? Is it so you can have one function which takes both types?
    Readability and maintainability. The trick is that I will only have one structure (it is not as simple as char name and data, it has MANY more parts to it). So, in the effort to abstract the data, the first byte of the structure becomes problematic for me.

    One who is more knowledgeable than I suggested that I just make the first byte descriptor more descriptive and specific so that I'll not stomp any other names in the name space. To that end, I'll make my identifier long and mixed case which will make this puppy special (I don't use mixed case identifiers usually -- That takes too long).

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Yeah. I don't get why you'd want that either... just come up with a common name for name/id... It compiles fine for me, anyway (VC '03), though intellisense (while not infallable) doesn't catch the name member of the name variable. Just calls it id.
    Just call it tag and live with it!

    (Posted afterI read bithub's response, and before Kennedy's second)

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Kennedy View Post
    Readability and maintainability.
    Hmmm. But in what sense is your conception here superior, "readability and maintainability"-wise, compared to the first example in the OP:
    Code:
    struct something {
            char id;
            char data[100];
    };
    
    struct somethingelse {
            char name;
            char data[100];
    };
    Especially since these have two distinct names -- or do you intent to #define aliases there too?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    What's wrong with a union?

    Code:
    struct something {
            union {
    		char id;
    		char name;
    	};	
            char data[100];
    };
    
    int main(void) {
    	struct something x;
    	x.id = 'x';
    	x.name = 'y';
    	return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Sebastiani View Post
    What's wrong with a union?
    I don't want to add in another . to my code. . . but then, I didn't realize that it would be acceptable to have no name, thus no additional . in the code. That's really cool and that is JUST what I was wanting.

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    A union would be the way to go, but unless I mis understood the logic by what Sebastiani showed, if you redefine the x union your x.name will overwrite x.id data.

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by slingerland3g View Post
    A union would be the way to go, but unless I mis understood the logic by what Sebastiani showed, if you redefine the x union your x.name will overwrite x.id data.
    Exactly! That is the whole point. The "real" world use would be something like this:
    Code:
    struct data {
            union {
                    uint8_t id;
                    uint8_t name;
            };
            uint32_t some, other, stuff;
            uint16_t etc, etc1, etc2;
    };
    
    int main(void)
    {
            struct data type0, type1;
            type0.id = 42;
            type1.name = 'a';
            <more>
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 05:03 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM