phaedrus said:
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?
Not really.
I think you're getting hung up on the distinction between a *type* and
an *object* (variable) of that type.
Does the phrase "a struct" refer to a type or an object? Does the
phrase "an array" refer to a type or an object? Or, in either case,
does it refer to a value, which is yet another different thing?
I'm not going to give you a definite answer to any of those questions;
rather, I'm going to argue that they're not particularly important
questions.
Think of "array" and "struct" as adjectives, not nouns. You can have
an array type, an array object, an array value, a struct type, a
struct object, a struct value. And likewise for integers, pointers,
unions, et cetera.
(Note: I use the word "object" rather than "variable" because
it's more precise. An object is, by definition, a "region of
data storage in the execution environment, the contents of which
can represent values"; it typically, but not always, has a type
associated with it. The word "variable" is more ambiguous.
Is an object declared with "const" a variable? It can't vary,
can it? Is an object created by a call to malloc() a variable?
We don't have a name for it; some people would call it a variable,
some wouldn't. It's another way we can get hung up on terminology,
when it's the underlying concepts that are important.)
This:
struct foo { int x; int y; };
is a type declaration; it creates a struct type. This:
struct foo obj = { 10, 20 };
is an object definition; it creates an object of that type. The
*value* of that object (until you change it) is a composite value
consisting of the values of its members, 10 and 20.
Once you think of it this way, I suggest that the answers to all your
questions become fairly obvious. A struct type can be thought of
as a "template" (using the word in the English sense, not the C++
sense). As a type, it holds no data. It's a specification for a
data object; you can create arbitrarily many objects of that type.
A struct object (an object of a struct type) occupies some amount
of storage and holds some value, which may or may not change as
the program executes. And exactly the same applies to array types,
objects, and values.
Having said all that, people commonly do use "struct" and "array"
without qualification, as nouns. The phrase "a struct" can refer
to a struct object or a struct object, or even a struct value.
Likewise for "an array", though I suppose that refers more commonly
to an object rather than a type for some reason.
Most of the time, either the meaning is fairly obvious from the
context, or it doesn't matter much. If you find the unqualified
word "struct" or "array" confusing, just be more explicit, and
if someone else uses it in an unclear manner, just ask.