Two questions about the following lines of code

C

Chad

#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?

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.

Chad
 
C

CBFalconer

Chad said:
.... snip ...

1) Is 'struct Time' a dynamic structure in this case?
No.


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.

Probably to make some sort of point, that is missing in your
elucidation. It does illustrate tagging what the valid
interpretations of the structure are.
 
P

pete

Chad said:
#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.
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.

You can read the tag to see what type of data is in the union.
 
C

Chad

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;
};
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.

Chad
 
C

CBFalconer

Chad said:
.... snip ...

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

I consider it to be something described by linkage. E.g. a list,
described by a pointer to the first entry, and terminated by a NULL
next pointer.
 
P

pete

Chad said:
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;
};


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.

The code as shown, doesn't read the tag to see
what the last data type written to the union was,
but the tag enables you to write code that does.
 
J

Justin Spahr-Summers

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.

Are you perhaps confused by the enumeration? An enumeration defines
integer constants by the names you provide between the braces (in this
case, DAYS and HOURSMINUTES, corresponding to 0 and 1, respectively,
although more are allowed). The code that "sets" the type of the
structure assigns one of these symbols (DAYS or HOURSMINUTES) to the
"tag" field and only modifies the intended field of the union. The
code that has to determine the value of the structure reads the "tag"
field and sees which enumeration value it contains (again, DAYS or
HOURSMINUTES) and then makes sure to read only the field of the union
that was last written.
 
B

Barry Schwarz

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.

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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top