Thread: Structs and arrays...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    73

    Structs and arrays...

    Hey everyone! I was wondering if you had a struct...
    Code:
    struct Map
    {
        int MyArray[24][40];
        int Enemies[64];
    }
    ...and if i pass this array by value...

    Code:
    int ManipStructWArray( Map MyMap )
    {
        int num = MyMap.MyArray[12][33];
        return 0;
    }
    ...is this legal? How about by reference?...

    Code:
    int ManipTheStruct( Map &MyMap )
    {
        int num = MyMap.MyArray[9][3];
        return num;
    }
    I need to know this because I have a test on structs and vectors on Saturday, and the only compilers I have access to are at the school... preferably passing by reference would be nice. Is what I did in that code legal?

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    yes, you could pass a struct by reference;

    but don't forget the semicolon after the closing bracket of your structure declaration:
    Code:
    struct Map
    {
        int MyArray[24][40];
        int Enemies[64];
    };

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    In this case, the default copy constructor works fine because you have static arrays, so the pass by value is ok (but inefficient). The pass by reference is of course better unless you really mean to make a copy. Passing structures or classes into functions is usually done by const reference unless there is a reason to do otherwise.

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    >>Passing structures or classes into functions is usually done by const reference<<

    yes, very important point which I forgot to mention!

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Pass by value is allowed, but pass by reference is more efficient, especially for such a large structure.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Pass by value is allowed
    you mean ...
    Code:
    int ManipStructWArray( Map MyMap )
    and
    Code:
    int ManipTheStruct( Map &MyMap )
    are both correct ?

    blow me ... ...

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Both are valid, but they are different and should be used in different situations. There is a third common form also using a const reference.

    Code:
    int ManipTheStruct( const Map &MyMap )
    The code above passes a const reference to the function. This means that the structure is not copied at all, AND it means that the data in the structure will not be changed (technically you can get around that by doing bad things, but from a design perspective this is accurate). Using a const reference for structs is common as I said above, because it is efficient (no copying) and doesn't change the data.

    Code:
    int ManipTheStruct( Map &MyMap )
    The code above passes a non-const reference. This form is used when you want to change the data inside the structure. Given that the function name is ManipTheStruct, it is quite possible that this is approriate. It is efficient because it does not copy the data in the struct, but it is not as safe as the const reference in protecting that data, so it is generally only used when you need to change something. Note that if you pass a struct into this function, you would expect the data in that struct to be different when the function returns.

    Code:
    int ManipTheStruct( Map MyMap )
    The code above passes the struct by value. A copy is made of all the data in the structure. This is generally inefficient. It has the advantage of not modifying the structure passed into it, but that advantage can be obtained by using a const reference. The situation where this is most often used is when the code inside needs to modify the data in the structure, but doesn't want those modifications to affect the structure passed in. You could use a const reference and make a copy inside the function, or you could just purposefully pass by value to force the copy up front.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocatable arrays in CLASSes and STRUCTS
    By simone.marras in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2009, 10:50 AM
  2. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  3. arrays in structs
    By *DEAD* in forum C Programming
    Replies: 3
    Last Post: 11-25-2006, 07:35 AM
  4. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 07:38 AM
  5. dynamic arrays of structs...
    By matheo917 in forum C++ Programming
    Replies: 8
    Last Post: 12-14-2002, 06:57 AM