![]() |
|
|
|
#1 |
|
Hi guys,
I've got so far as structs in re-learning C and have hit a bit of a wall. The online resources on the subject seem to contradict each other in a way that I've not seen before. There's just ONE point I'd like clarified, please. This is my assertion: A 'struct' in C is not actually a modularised cluster of disparate data. It's simply a *template* for a complex variable of type struct, and it is this *variable* which actually forms the modularised cluster of disparate data. The struct itself is just an empty shell; merely a *specification* for a data object. This data object is what we call a variable of type struct. A struct alone therefore doesn't hold any data; it is not like an array. Arrays can store data, structs by themselves don't. Have I got that right? Thanks. phaedrus |
|
|
|
|
#2 |
|
Posts: n/a
|
On 5 Nov, 11:44, phaedrus <orion.osi...@virgin.net> wrote:
> Hi guys, > > I've got so far as structs in re-learning C and have hit a bit of a > wall. The online resources on the subject seem to contradict each > other in a way that I've not seen before. There's just ONE point I'd > like clarified, please. > > This is my assertion: > > A 'struct' in C is not actually a modularised cluster of disparate > data. It's simply a *template* for a complex variable of type struct, > and it is this *variable* which actually forms the modularised cluster > of disparate data. The struct itself is just an empty shell; merely a > *specification* for a data object. This data object is what we call a > variable of type struct. A struct alone therefore doesn't hold any > data; it is not like an array. Arrays can store data, structs by > themselves don't. > > Have I got that right? > > Thanks. No, I don't think that's right. An array can't store data; the data is stored in its individual ELEMENTS. A struct can't store data; the data is stored in its individual FIELDS. The elements of the array are referenced by their subscripts; the fields of the struct are referenced by their names. There's a difference, but I think it's a much smaller difference than you think it is. -- bert |
|
|
|
#3 |
|
Posts: n/a
|
In article
<ee58ab7e-f6b4-4f81-9b18->, phaedrus <> wrote: > On Nov 5, 12:56*pm, bert <bert.hutchi...@btinternet.com> wrote: > > > No, I don't think that's right. *An array can't store data; the > > data is stored in its individual ELEMENTS. *A struct can't store > > data; the data is stored in its individual FIELDS. *The elements > > of the array are referenced by their subscripts; the fields of > > the struct are referenced by their names. *There's a difference, > > but I think it's a much smaller difference than you think it is. > > -- > > OK, point taken. Let me clarify, then. > > The data in an array is stored in its elements. > The data for a struct is stored in the member fields of a structure > VARIABLE. > > Is that correct? As I recall, if I do: struct wiggy { int a; int b; } Then I've reserved some actual space (two ints-worth). But you can also do: typedef struct wiggy { int a; int b; } which just defines a new type, wiggy, and reserves no space. It's not until later that I do: int p; int q; wiggy diggy; that I've now reserved space for 4 ints. I *think* that's what I used to do. -- Tim "That excessive bail ought not to be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted" -- Bill of Rights 1689 Tim Streater |
|
|
|
#4 |
|
Posts: n/a
|
On Nov 5, 12:56*pm, bert <bert.hutchi...@btinternet.com> wrote:
> No, I don't think that's right. *An array can't store data; the > data is stored in its individual ELEMENTS. *A struct can't store > data; the data is stored in its individual FIELDS. *The elements > of the array are referenced by their subscripts; the fields of > the struct are referenced by their names. *There's a difference, > but I think it's a much smaller difference than you think it is. > -- OK, point taken. Let me clarify, then. The data in an array is stored in its elements. The data for a struct is stored in the member fields of a structure VARIABLE. Is that correct? phaedrus |
|
|
|
#5 |
|
Posts: n/a
|
On 5 Nov, 12:29, phaedrus <orion.osi...@virgin.net> wrote:
> On Nov 5, 12:56*pm, bert <bert.hutchi...@btinternet.com> wrote: > > No, I don't think that's right. *An array can't store data; the > > data is stored in its individual ELEMENTS. *A struct can't store > > data; the struct declaration declares a type > > the data is stored in its individual FIELDS. *The elements > > of the array are referenced by their subscripts; the fields of > > the struct are referenced by their names. *There's a difference, > > but I think it's a much smaller difference than you think it is. > > OK, point taken. Let me clarify, then. > > The data in an array is stored in its elements. > The data for a struct is stored in the member fields of a structure > VARIABLE. > > Is that correct? arrays are variables too... There are types int, struct S {float f} there are declarations, which describe what something is but without setting aside storage extern int i; extern int a[]; extern struct S s; and there are definitions which set aside store int i; int a [10]; struct S s; the slight confusion is that "naked" array types are hardly used (i'm not even sure if they exist). typedefs add an additional layer of confusing as despite their name they don't define types but type aliases. Nick Keighley |
|
|
|
#6 |
|
Posts: n/a
|
I've come across this in one of the online tutorials and it seems to
explain the concept better than anything else I've yet seen: [begins...] A structure is declared by making a blank template for a variable package. This is most easily seen with the help of an example. The following statement is actually a declaration, so it belongs with other declarations, either at the head of a program or at the start of a block. struct PersonalData { char name[namesize]; char address[addresssize]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; This purpose of this statement is to create a model or template to define what a variable of type struct PersonalData will look like. It says: define a type of variable which collectively holds a string called name, a string called address and three integers called YearOfBirth, MonthOfBirth and DayOfBirth. Any variable which is declared to be of type struct PersonalData will be collectively made up of parts like these. The list of variable components which make up the structure are called the members of the structure: the names of the members are not the names of variables, but are a way of naming the parts which make up a structure variable. (Note: a variable which has been declared to be of type struct something is usually called just a structure rather than a structure variable. The distinction is maintained here in places where confusion might arise.) The names of members are held separate from the names of other identifiers in C, so it is quite possible to have variable names and struct member names which are the same. Older compilers did not support this luxury. At this stage, no storage has been given over to a variable, nor has any variable been declared: only a type has been defined. Having defined this type of structure, however, the programmer can declare variables to be of this type. For example: struct PersonalData x; declares a variable called x to be of type struct PersonalData. phaedrus |
|
|
|
#7 |
|
Posts: n/a
|
In article <hcurbg$jej$>,
Richard <rgrdev_@gmail.com> wrote: > Tim Streater <> writes: > > > In article > > <ee58ab7e-f6b4-4f81-9b18->, > > phaedrus <> wrote: > > > >> On Nov 5, 12:56Â*pm, bert <bert.hutchi...@btinternet.com> wrote: > >> > >> > No, I don't think that's right. Â*An array can't store data; the > >> > data is stored in its individual ELEMENTS. Â*A struct can't store > >> > data; the data is stored in its individual FIELDS. Â*The elements > >> > of the array are referenced by their subscripts; the fields of > >> > the struct are referenced by their names. Â*There's a difference, > >> > but I think it's a much smaller difference than you think it is. > >> > -- > >> > >> OK, point taken. Let me clarify, then. > >> > >> The data in an array is stored in its elements. > >> The data for a struct is stored in the member fields of a structure > >> VARIABLE. > >> > >> Is that correct? > > > > As I recall, if I do: > > Why is everything "as you recall"? Because I haven't used C for 20 years, as I've pointed out before. Sit up straight etc etc. > > struct wiggy > > { > > int a; > > int b; > > } > > > > Then I've reserved some actual space (two ints-worth). But you can also > > do: > > > > typedef struct wiggy > > { > > int a; > > int b; > > } > > > > which just defines a new type, wiggy, and reserves no space. It's not > > until later that I do: > > > > int p; > > int q; > > wiggy diggy; > > > > that I've now reserved space for 4 ints. > > > > I *think* that's what I used to do. > > Why would you offer advice in group of C specialists if you only "think" > that's what you used to do? Why shouldn't I? If I'm wrong, (certainly not excluded, see above) then I expect I'll be corrected by those who know better. Happy now? -- Tim "That excessive bail ought not to be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted" -- Bill of Rights 1689 Tim Streater |
|
|
|
#8 |
|
Posts: n/a
|
In article <>,
Richard Heathfield <> wrote: > In <timstreater->, Tim > Streater wrote: > > > In article > > <ee58ab7e-f6b4-4f81-9b18->, > > phaedrus <> wrote: > > > <snip> > > >> The data in an array is stored in its elements. > >> The data for a struct is stored in the member fields of a structure > >> VARIABLE. > >> > >> Is that correct? > > > > As I recall, if I do: > > > > struct wiggy > > { > > int a; > > int b; > > } > > > > Then I've reserved some actual space (two ints-worth). > > You recall incorrectly. The above is just a type definition. It > reserves no storage. If you'd done this: > > struct wiggy > { > int a; > int b; > } earwig; > > you'd have reserved (at least) two ints'-worth of storage. Or you > could do it like this: > > struct wiggy > { > int a; > int b; > }; > > struct wiggy earwig; > > > But you can also do: > > > > typedef struct wiggy > > { > > int a; > > int b; > > } > > > > which just defines a new type, wiggy, > > No, it defines a new type, struct wiggy, and then gives (or rather, in > your case, fails to give) that type a synonym. Ha! Richard - thanks for the correction. -- Tim "That excessive bail ought not to be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted" -- Bill of Rights 1689 Tim Streater |
|
|
|
#9 |
|
Posts: n/a
|
On 2009-11-05, phaedrus <> wrote:
> I've got so far as structs in re-learning C and have hit a bit of a > wall. The online resources on the subject seem to contradict each > other in a way that I've not seen before. There's just ONE point I'd > like clarified, please. > > This is my assertion: > > A 'struct' in C is not actually a modularised cluster of disparate > data. It's simply a *template* for a complex variable of type struct, > and it is this *variable* which actually forms the modularised cluster > of disparate data. The struct itself is just an empty shell; merely a > *specification* for a data object. This data object is what we call a > variable of type struct. A struct alone therefore doesn't hold any > data; it is not like an array. Arrays can store data, structs by > themselves don't. > > Have I got that right? I don't think so. Except I think you do, but you are using the terminology in a surprising way. IMHO, "a struct" refers both to the object of the type and to the type. Consider: "long int" is a type. "long int x;" declares an object of that type. "struct foo" is a type. "struct foo x;" declares an object of that type. But I would say that x is "a struct foo", and is "the struct itself" just as much as the type declaration is. -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet- http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! Seebs |
|
|
|
#10 |
|
Posts: n/a
|
On 2009-11-05, phaedrus <> wrote:
> The data in an array is stored in its elements. > The data for a struct is stored in the member fields of a structure > VARIABLE. > > Is that correct? No. struct foo { ... }; void *v = malloc(10 * sizeof(struct foo)); struct foo *p = v; p[3] <-- a struct, but not a struct variable. The only variables there are p and v. Neither is a struct. p is a pointer, not a struct. -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet- http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! Seebs |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Motherboard general question | Jimmy Sledge | Computer Information | 8 | 03-10-2007 08:37 AM |
| Question on learning "Networking"... | DoubleEntendre | Computer Support | 15 | 03-11-2006 11:28 AM |
| hd partition question | rabbit | Computer Support | 6 | 01-12-2006 04:05 AM |
| Wireless PEAP/MSCHAPV2 client programming question | Jim Howard | Wireless Networking | 6 | 07-02-2005 12:53 PM |
| xp question | Barry | Computer Support | 18 | 12-31-2003 06:06 AM |