On Wed, 22 Aug 2007 18:39:44 -0700, Chad <cd*****@gmail.comwrote:
>On Aug 22, 6:12 pm, pete <pfil...@mindspring.comwrote:
>Chad wrote:
#include <stdio.h>
enum Type {DAYS, HOURSMINUTES};
struct Days {
int num_days;
};
struct HoursMinutes {
int num_hours;
int num_minutes;
};
struct Time {
enum Type tag;
union {
struct Days days;
struct HoursMinutes hm;
} u;
};
void print(struct Time ti)
{
int minutes;
if (ti.tag == DAYS) {
minutes = ti.u.days.num_days * 24 * 60;
}
else {
minutes = ti.u.hm.num_hours *
60 + ti.u.hm.num_minutes;
}
printf("number of minutes = %d\n", minutes);
}
int main(void)
{
struct Time ti;
ti.tag = DAYS;
ti.u.days.num_days = 10;
print(ti);
ti.tag = HOURSMINUTES;
ti.u.hm.num_hours = 15;
ti.u.hm.num_minutes = 59;
print(ti);
}
1)Is 'struct Time' a dynamic structure in this case?
I don't know what "dynamic structure" means.
Okay, I don't the correct wording. The extent of my formal computer
programming was 6 weeks of FORTRAN programming on OpenVMS back in the
early 90's. Anyhow, what I meant by "dynamic structure" is that in one
case the structure could be
struct Time {
enum DAYS;
union {
struct Days days;
struct HoursMinutes hm;
} u;
};
and in anther case, we could have
struct Time {
enum HOURSMINUTES;
union {
struct Days days;
struct HoursMinutes hm;
} u;
};
I think if you want to imagine the structure as being in one of its
"possible states", try along the lines of (punctuation deliberately
omitted since the declarations are not valid)
with tag set to DAYS:
struct Time{
enum Type tag
struct Days u.days}
with tag set to HOURSMINUTES:
struct Time{
enum Type tag
struct HoursMinutes u.hm}
struct Time will always hold a enum Type tag and a union u. union u
will hold only either a struct Days **or** a struct HoursMinutes,
depending on the value in tag.
Since every non-trivial union contains multiple overlaid objects of
which at most one is in use at any given time, the same can be said
for any struct containing such a union. Dynamic is not one of the
adjectives normally used in this situation but since the type of the
value in the union can change over time it is not completely incorrect
either.
>
2)I'm assuming this
struct Time {
enum Type tag;
union {
struct Days days;
struct HoursMinutes hm;
} u;
};
Is some kind of tagged union. I don't see why the author constucted
the structure in this way.
The word tag has a special meaning in the standard. Time is the tag
of the struct being defined. The union member does not have a tag.
Hopefully, why the author chose this construction is discussed in the
accompanying text. Otherwise, one is forced to guess.
>>
You can read the tag to see what type of data is in the union.
Okay, I really don't see how it reads the tag to see what type of data
is in the union. I'm suspecting I'm going to have to break out the
debugger and step through the code to see what you are talking about.
Look at the if statements in the print function. They don't read
**the tag** but they do read **the member named tag** to determine
which member of u is valid to evaluate.
Remove del for email