Thread: 2 Dimenstional Array vs Array of Structures

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    2 Dimenstional Array vs Array of Structures

    To me an array of a structure or class seems a lot more clear to understand than a 2 dimenstional array. After all, when the array is of type struct, the structure member data ( which is like the second dimension in a 2 dimenstional array ) is accessed by a name rather than a index number making it easier to understand read, and code. Example:

    Code:
    struct PersonalInfo
         {
         int age, yearOfBirth;
         } p1[2];
    is accessed p1[0].age = 12;

    as opposed to:

    Code:
     int personalInfo[2] [2];
    is accessed personalInfo[0] [0] = 12;

    Am i right?
    Is there any time that using a 2 dimensional array would be more advantageous than using an array of a structure/class (besides saving lines of code)
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You're definitely right. It's much easier to read and understand a structure with proper names than learning that index 279 is one specific attribute and 123 another.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Is there any time that using a 2 dimensional array would be more advantageous than using an array of a structure/class (besides saving lines of code)
    What about when you just need a matrix of some sorts. Like if you were laying out a chessboard. Then a two dimensional array would be better and easier to understand.

    Also when passing an array of strings you are passing a two dimensional array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  2. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  3. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  4. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 05:54 PM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 03:39 PM