distinction between structure and array

This is a discussion on distinction between structure and array within the C++ Programming forums, part of the General Programming Boards category; Hi Let's make a comparison between an array and structure. An array is a kind of macro-variable which contains in ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    distinction between structure and array

    Hi

    Let's make a comparison between an array and structure. An array is a kind of macro-variable which contains in itself very much closely related variables. e.g. a human could be an array (or, macro-molecule) and all humans could be its elements (or, individual variables). On the other hand a structure is also a macro-variable which combines attributes of a single object etc into one whole. Please correct me. Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,801
    O_o

    Dude, why don't you just get a book?

    Soma

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,853
    It might be easier if your found a good introductory textbook, rather than making random guesses about what various terms mean.

    A data structure is a way of organising (i.e. storing in a structured manner) a set of data values of various types.

    An array is a collection of values, all of the same type, stored sequentially in memory.

    Your term "macro-variable" is meaningless.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by phantomotap View Post
    O_o

    Dude, why don't you just get a book?

    Soma
    Dude,

    I have a book! Was just trying to confirm if my conceptual understanding was correct.

    Best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think the analogy with "macro-molecules" would be more appropriate to objects, which are structures, and not arrays.

    But it is still not very useful, and your conceptual understanding: "On the other hand a structure is also a macro-variable which combines attributes of a single object etc into one whole," is not very meaningful either. So do you mean the difference between an array and a structure is that an array does not combine something into a whole?

    This is just plain the wrong kind of language to use, focussing on the wrong things. So IMO: your conceptual understanding is NOT correct.

    C/C++ arrays are homogeneously typed, contiguous data structures. Describing them as "macro" anything is wrong because it implies an array is organized in some some kind of meaningful and dynamic way. It is not. It is just one variable after another, all of exactly the same sort, in a single sequence: 1st, 2nd, 3rd, 4th. That's it.

    Structs are also contiguous in memory but can be heterogenous; they may contain different types, and in C++, blocks of executable code (function definitions). However, structs must be pre-defined, they cannot have members added dynamically, where as an array can. But a struct can contain an array (and you can have an array of structs).
    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
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    a better way of looking at it might be to consider a person to be an object of type Human, where Human is a struct, with members (which may also be objects) of type Head, Arm, Leg, Heart, Lung, etc. The Human population of the earth could be seen as an array of Human objects. but this is still just a very general, and somewhat inaccurate analogy (as are most analogies).

    an array's elements are always all of the same (base) type, and are referenced by using either pointer arithmetic or the array index [] operator.
    a struct may contain elements of many types, and its members are referenced with the dot or -> operators.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    An array is a primitive container of a single type. It's like a row of people. There are also standard objects that function this way, which generally should be used in favor of arrays. These objects are classes that behave like arrays.

    A class or struct is a bunch or related data lumped together into one type. In object oriented programming, classes abstractly represent objects or things that function like objects.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    in keeping with the chemical analogy:

    array is to polymer as object (a struct or class) is to molecule

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  2. Array with a structure
    By Jenna_Javason in forum C Programming
    Replies: 7
    Last Post: 10-09-2007, 06:58 AM
  3. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  4. structure array
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-29-2002, 11:20 AM
  5. structure array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-20-2002, 08:30 AM

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