473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Two questions about the following lines of code

#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

Aug 23 '07 #1
7 1235
Chad wrote:
>
.... 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Aug 23 '07 #2
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.
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.

--
pete
Aug 23 '07 #3
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;
};
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.
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
Aug 23 '07 #4
Chad wrote:
>
.... 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 23 '07 #5
Chad wrote:
>
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;
};
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.

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.

--
pete
Aug 23 '07 #6
On Aug 22, 8:39 pm, Chad <cdal...@gmail.comwrote:
On Aug 22, 6:12 pm, pete <pfil...@mindspring.comwrote:
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.
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.

Aug 23 '07 #7
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
Aug 26 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: TZoner | last post by:
1) Can one find the location of the 'Transaction Log' at: <Hard Disk>\Program Files\Microsoft SQL Server\MSSQL\Data\MyDb_Log.ldf? 2) Is it safe to delete it, as SQL will create a new Transaction...
4
by: Greener | last post by:
May I ask you the following? Two questions about the following block of code: 1) How to open the file in NON-ReadOnly mode? I tried many things, but none of them was working. 2) Any problems...
4
by: Venn Syii | last post by:
I've searched all the forums but cannot find an answer to this question. I do the following: vector<MyClass*> myClassList; Later in the program I try to add to myClassList with a...
5
by: randomblink | last post by:
Alright... Does anyone know if you can create a class that has a constructor? I would like to create a BUTTONMANAGER class that handles my buttons for me... I am doing this in Access for those...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
0
by: H | last post by:
Hello, some newbie questions here ... 1. Is there any way to set a maximum number of lines on a textbox ? I'd like my box to only be have to have 30 lines of text in it. How can this be done ? ...
13
by: M.Siler | last post by:
Let me clarify from my last post. I am not using these 4 questions as the sole screening method. Currently in, the Tampa Bay area (Florida) there is an extreme shortage of C# developers. We have...
1
by: carly | last post by:
hi, i've been solving these multiple choice questions, and i'd like to know if what i answered is right ot wrong......... just to double check :) I) Consider the code given below. ...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
40
by: aarklon | last post by:
Hi all, I was reading the book "C interview Questions" By J. Rajaram published in india by firewall media, after going through the book i have the following doubts 1) why the following...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.