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?
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.
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 */
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?
The -> operator is used to access a member of a structure via a pointer. For example:
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.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 */
from above:
ptr->a = 7;
note that the -> notation is a shorthand for this
(*ptr).a = 7;
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
Originally posted by ucsbme
the most important difference seem to be ignored.
where is quzah ... n all others????????
So how was the most important difference ignored?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.
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.Originally posted by The Dog
So how was the most important difference ignored?
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.