structs and elements by reference?

This is a discussion on structs and elements by reference? within the C++ Programming forums, part of the General Programming Boards category; hi, i have the following code: BYTE is type def as unsigned char. Code: // header file: typedef unsigned char ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    34

    structs and elements by reference?

    hi, i have the following code:
    BYTE is type def as unsigned char.

    Code:
    // header file:
    
    typedef unsigned char BYTE;
    
    struct LIGHTS
        {
        BYTE set1; // element 0
        BYTE set2; // element 1
        BYTE set3; // element 2
        };
    
    // inside main:
    
        LIGHTS lights_array;
        BYTE size;
    
        lights_array.set1 = 31;
        lights_array.set2 = 32;
        lights_array.set3 = 33;
            
        size = lights_array.set1 ; // get value of 'set1'
    --------
    problem is, i want to be able to access each element using a number, not the name... e.g.:

    Code:
        size = lights_array.set1 ; // retruns 31
        size = lights_array.set3 ; // returns 33
    would become (i know this is wrong) :
    Code:
        size = lights_array[0] ; // get value of element 0 (set1) return 31
        size = lights_array[1] ; // get value of element 1 (set2) return 32
        size = lights_array[2] ; // get value of element 2 (set3) return 33

    how can i do this???

    Joe
    #

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    If you want to mimic an array, then why not use one?

    BYTE lights_array[3];

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Change the LIGHTS struct a bit

    Code:
    struct LIGHTS
    {
        BYTE set1; // element 0
        BYTE set2; // element 1
        BYTE set3; // element 2
    
        BYTE& operator[](unsigned id)
        {
           if (id == 0)
             return set1;
           else if (id == 1)
             return set2;
           else if (id == 2)
             return set2;
           else
             throw "LIGHTS::[]  : bad id";
        }
    };
    Although I'd probably change the struct more than that

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    i need to use int to access the elements because im going to use a for loop to get the data from each element:

    Code:
    for(x=1; x<number_of_elements; x++) // skip element 0
    {
    size = lights_array[x]; // doesnt work
    DoData(size);
    }
    element 0 must NOT be read by the for loop as it is an array (not shown here to simplify the question)

    joe
    #

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    the question is very much simplified, the actual struct contains 57 elements of all different types and identifier names, i have 7 structs.

    i need code that is "identifier name independent", e.g. i dont have to use the identifier name to access the data.

    This enables me to access the same element (element 3 for example) in several different structs without having to type out the element name each time. Also each struct has a different number of elements and each element has a different name.

    (if that makes sence)

    joe
    #

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Umm, perhaps you should consider restructuring your code.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    so does this mean that it is not possible to index the elements?

    Can i get a pointer to the 1st element, then increment the pointer by the sizeof each element until i get to the one i want?

    There must be a solution! Windows uses structs all over the place!

    joe
    #

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Originally posted by Sang-drax
    Change the LIGHTS struct a bit

    Code:
    struct LIGHTS
    {
        BYTE set1; // element 0
        BYTE set2; // element 1
        BYTE set3; // element 2
    
        BYTE operator[](unsigned id)
        {
           if (id == 0)
             return set1;
           else if (id == 1)
             return set2;
           else if (id == 2)
             return set2;
           else
             throw "LIGHTS::[]  : bad id";
        }
    };
    Although I'd probably change the struct more than that
    Look above and see your answer. Just overload the [] operator. I'm a tad confused with the naming of the variable "size". Wouldn't it be more fitting to be "value"? You know the size, it's a BYTE.

    Tweaked what Sang-drax posted
    Code:
    struct LIGHTS
    {
        BYTE set1; // element 0
        BYTE set2; // element 1
        BYTE set3; // element 2
    
        BYTE operator[](unsigned id)
        {
           if (id == 0)
             return set1;
           else if (id == 1)
             return set2;
           else if (id == 2)
             return set3;
           else
             throw "LIGHTS::[]  : bad id";
        }
    };
    
    void main ()
    {
    	LIGHTS x;
    	x.set1 = 5;
    	x.set2 = 10;
    	x.set3 = 15;
    
    	for (int y=0; y < 3; y++)
    		cout << (int) x[y] << " ";
    }
    Last edited by Divx; 10-06-2002 at 03:21 PM.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main ()
    Naughty naughty, look at this mess. I expect to see your code cleaned up next time I look in here young man.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    void smoid

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void smoid
    My thoughts exactly, int seems happier just before main than void does. And the "void main gestapo" won't go after you.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    OK, it worked, many thanks for everyones efforts. The pointer idea doesnt work because the 1st element is an array of 27 ints!! but this code did:

    Code:
    struct LIGHTS
    {
        BYTE set1; // element 0
        BYTE set2; // element 1
        BYTE set3; // element 2
    
        BYTE operator[](unsigned id)
        {
           if (id == 0)
             return set1;
           else if (id == 1)
             return set2;
           else if (id == 2)
             return set3;
           else
    //         throw "LIGHTS::[]  : bad id";
               return 255;
        }
    };
    
    void main ()
    {
    	LIGHTS x;
    	x.set1 = 5;
    	x.set2 = 10;
    	x.set3 = 15;
    
    	for (int y=0; y < 3; y++)
    		cout << (int) x[y] << " ";
    }
    I have no idea how it works but it does what i need(except the 'throw' because the compiler says its "reserved for future use" (VC++ 1.53)).
    -- yes, i do have VC6 and VS.net, but i prefer VC1.53 for messing with small code examples!

    I cant find this overloading(?) in any of my books... can someone explain it a bit? if not, nevermiind, i'm moving on again!

    2 more things...

    1/ Is there a 'c' equlivent?

    2/ What's the beef between void main() and int main()??? Does it really matter?!

    joe
    #

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I cant find this overloading(?) in any of my books
    You need to get better books then, operator overloading is an important piece of C++ and every book I've seen at least mentions it.

    >Is there a 'c' equlivent?
    C doesn't support operator overloading or member functions, you would have to use another solution to get the equivalent code in C.

    >What's the beef between void main() and int main()???
    void main is wrong, anyone who knows this instinctively rears back in horror when they see void main.

    >Does it really matter?!
    Yes, void main makes the entire program undefined while int main is correct and predictable.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    does the OS really check the return value from main() ?

    I returned 4 different values:

    void, -1, 0, 1

    and all have the same effect - nothing!!

    So what's the point??

    joe
    #

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by kybert
    does the OS really check the return value from main() ?

    I returned 4 different values:

    void, -1, 0, 1

    and all have the same effect - nothing!!

    So what's the point??

    joe
    #
    Its a point of neatness....

    On many compilers, the C runtime checks the return of main and then assigns that value to an int.....this int is then passed back to the OS with the method used to terminate the process...

    The return is handy for scripting and such as scripts can rely on the return of a process to branch off in the script.....

    Also from a more formal view, void main is undefined in std C++....

    So if both practicality and the standard say dont use it.....you should avoid it IMO

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Node type with a choice of elements
    By quizkiwi in forum C++ Programming
    Replies: 7
    Last Post: 09-01-2006, 10:41 AM
  2. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 05:05 PM
  3. Passing structs as parameters
    By 182 in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2006, 10:51 PM
  4. Vector of Structs Question
    By gamer4life687 in forum C++ Programming
    Replies: 6
    Last Post: 09-09-2005, 11:13 PM
  5. Allocating dynamic memory for structs
    By Nevyn in forum C Programming
    Replies: 4
    Last Post: 09-17-2001, 12:54 PM

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