structures and arrays. Whats the diff?

This is a discussion on structures and arrays. Whats the diff? within the C Programming forums, part of the General Programming Boards category; Hello, I understand that a structure is closely related to an array. One would think there are no real difference ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    structures and arrays. Whats the diff?

    Hello,
    I understand that a structure is closely related to an array. One would think there are no real difference between them. So what is the difference between these 2 or are they just a matter of which programming style you wish to use?
    Last edited by correlcj; 08-05-2002 at 04:30 PM.

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Structures and arrays are very different.

    An array is like a list of data elements, each element is referenced by it's own number. A structure (in C) is a grouping of data elements.

    Code:
    struct MyStruct {
          int a;
          int b;
    };
    
    struct MyStruct Instance;
    
    /* ... */
    
    Instance.a = 6;
    Instance.b = Instance.a + 2;
    Code:
    /* ... */
    int array [ 20 ];
    array [ 0 ] = 6;
    array [ 7 ] = 2552;
    /* etc */

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    An array is like a list of data elements, each element is referenced by it's own numb

    Well does this mean that between 2 structure definitions then e.g.,

    [code]
    struct city1
    {
    char name[50]
    int pop
    }
    c1;

    struct *name;
    int pop;
    {
    c2;

    does this mean then, the name is accessed via c1.name and in struct city2 the name is accessed via c2 -> name? I thought these were basically the same except it was a matter of preference, really?

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    The -> operator is used to access a member of a structure via a pointer. For example:

    Code:
    struct Structure {
         int a, b;
    };
    
    struct Structure reg;
    struct Structure *ptr;
    
    /* ... */
    
    reg.a = 6; /* access using dot operator */
    ptr->a = 7; /* because it's a pointer we use the -> operator */
    For example you may have a function that swaps two structures that takes two pointers to structs. You would use the -> operator rather than the normal dot access operator because they're pointers.

  5. #5
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Thumbs up that makes sense now!

    Thanks!

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    from above:

    ptr->a = 7;

    note that the -> notation is a shorthand for this

    (*ptr).a = 7;

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    struct vs array

    babu..
    the most important difference seem to be ignored.
    where is quzah ... n all others????????

    ok...

    while an array can store only similar datatypes (may be user defined or primitive), a structure can store different or same data types. it does not matter whether you store same or dissimilar datatypes in a structure.

    for example
    int a[10] means an collection of ten integers.
    char c[10] ,means a collection of ten characters.
    and struct aa { int a; }; struct aa struct_array[10] means an array of ten structures.

    AND

    struct aa { int a; char b; };
    so, a structure may be used as a collection of sililar or dissimilar collection of datatypes.


    though both of them share some common features like for example allocation of memory for the elements of an array and a structure are in continuous memory locations.

    hope this is clear
    ucsbme

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Originally posted by ucsbme
    the most important difference seem to be ignored.
    where is quzah ... n all others????????
    Originally posted by rmullen3
    Structures and arrays are very different.

    An array is like a list of data elements, each element is referenced by it's own number. A structure (in C) is a grouping of data elements.
    So how was the most important difference ignored?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by The Dog
    So how was the most important difference ignored?
    Probably because anyone that has read the first thing about structures knows that this is the very first thing you learn other than how to create one.

    I mean come on! Are they really even trying? If you've read anything on structures you know this answer. (And no, Dog, this is not a post in reply to you. It's to the original poster.)

    I purposefully ignored this thread, as I am with all of this posters threads.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Talking Thank You!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with arrays structures sorting.
    By pitifulworm in forum C Programming
    Replies: 42
    Last Post: 02-09-2009, 11:31 AM
  2. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  3. errors with arrays and structures
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 10:48 PM
  4. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 05:29 PM

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