Structure within a structure

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2781 Views - Last Post: 30 April 2010 - 07:09 PM Rate Topic: -----

#1 Guest_Andy*


Reputation:

Structure within a structure

Posted 28 April 2010 - 07:23 PM

Okay so I have an assignment for school to create a structure within a structure. the assignment was to write a program to add two yards, feet, and inches together and have a total in yards. Then a second part is to use add on to the program to find the area of a rectangle.

This is what I did and he said it is wrong but wouldn't tell me why and I don't understand what I am doing wrong

#include <stdio.h>		

typedef struct
{
	int yards, feet;
	float inches, total;

} yardsfeetincheslength;

typedef struct
{
	int yards, feet;
	float inches, total;

} yardsfeetincheswidth;

typedef struct
{
	yardsfeetincheslength;
	yardsfeetincheswidth;

} yardsfeetinches;

int main()
{
	yardsfeetinches d1, d2, d3, lw, total;

	printf("Please enter the two distances in inches, feet, and yards: \n");

	scanf("%f%d%d%f%d%d", &d1.inches, &d1.feet, &d1.yards, &d2.inches, &d2.feet, &d2.yards);
	
	d1.total = (d1.inches/12.0) + (d1.feet/3.0) + d1.yards;
	d2.total = (d2.inches/12.0) + (d2.feet/3.0) + d2.yards;
	d3.total = d1.total + d2.total;
	lw.total = d1.total * d2.total;

printf ("%.2f Total Yards One + %.2f Total Yards Two = %.2f Yards", d1.total, d2.total, d3.total);
printf ("\nThe length of %.2f times the width of %.2f equals a Area of %.2f ", d1.total, d2.total, lw.total);

}



Is This A Good Question/Topic? 0

Replies To: Structure within a structure

#2 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Structure within a structure

Posted 28 April 2010 - 07:35 PM

Notice that here
        int yards, feet;


when you declared two variables of type int, you specified names for the two variables

but here
typedef struct
{
        yardsfeetincheslength;
        yardsfeetincheswidth;

} yardsfeetinches;


you declared a variable of type yardsfeetincheslength and a variable of type yardsfeetincheswidth, but didn't give a name for either.

Also, do d1, d2, etc, have members inches, feet, yards? (No -- they have struct members, and those struct members have the inches, etc.)

This post has been edited by r.stiltskin: 28 April 2010 - 07:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Andy*


Reputation:

Re: Structure within a structure

Posted 28 April 2010 - 07:42 PM

so I need to change them to?
typedef struct
{
	int yardsfeetincheslength;
	int yardsfeetincheswidth;

} yardsfeetinches;

Was This Post Helpful? 0

#4 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Structure within a structure

Posted 28 April 2010 - 07:44 PM

Quote

reate a structure within a structure


i dont see any structs with in structs anywhere

typedef struct a
{
      typedef struct b
      {  
      };
};


Was This Post Helpful? 0
  • +
  • -

#5 taiku   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 92
  • Joined: 28-October 09

Re: Structure within a structure

Posted 28 April 2010 - 08:01 PM

Does that even work?
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Structure within a structure

Posted 28 April 2010 - 08:08 PM

View PostAndy, on 28 April 2010 - 10:42 PM, said:

so I need to change them to?
typedef struct
{
	int yardsfeetincheslength;
	int yardsfeetincheswidth;

} yardsfeetinches;


No. The way you have it now, yardsfeetincheslength is a type, and yardsfeetincheswidth is a type. But I don't know why you want to have different types for length and width. Why not have a measurement type containing the yards, feet, inches and total variables? And a rectangle type that contains two measurement members, as in:
measurement length;
measurement width;


Was This Post Helpful? 0
  • +
  • -

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Structure within a structure

Posted 28 April 2010 - 08:08 PM

Quote

Does that even work?


why wouldn't it?

This post has been edited by ImaSexy: 28 April 2010 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_Andy*


Reputation:

Re: Structure within a structure

Posted 28 April 2010 - 08:24 PM

idk I give up my code is so F***ed right now I cant even get it to run anymore

I am just gonna start from scratch tomorrow

thanks for everyones help I am sure I will be back tomorrow
Was This Post Helpful? 0

#9 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Structure within a structure

Posted 29 April 2010 - 05:55 AM

// good, you've defined a type
typedef struct {
	int yards, feet;
	float inches, total;
} yardsfeetincheslength;


// wait a minute, you're defining the exact same struct as a different type
typedef struct
{
        int yards, feet;
        float inches, total;

} yardsfeetincheswidth;

// you define a type, with a couple of other types inside
// however, what are their names?
typedef struct
{
        yardsfeetincheslength;
        yardsfeetincheswidth;

} yardsfeetinches;



First, consider typedefs:
struct YFIT {
	int yards, feet;
	float inches, total;
};

// now the typedefs
typedef struct YFIT yardsfeetincheswidth;
typedef struct YFIT yardsfeetinches;



Imagine the compiler takes the "struct YFIT" and replaces it with "struct { int yards, feet; float inches, total; };" in the typedef, which is essentially what's happening.

Howevever, I don't believe you want this. I believe, you want this:
typedef struct {
	struct { 
		int yards, feet; 
		float inches, total; 
	} yardsfeetincheslength, yardsfeetincheswidth;
} yardsfeetinches;



Or, to avoid that anonymous struct:

typedef struct { 
	int yards, feet; 
	float inches, total; 
} yardsfeetinchestotal;

typedef struct {
	yardsfeetinchestotal yardsfeetincheslength;
	yardsfeetinchestotal yardsfeetincheswidth;
} yardsfeetinches;



Hope this helps.


View PostImaSexy, on 28 April 2010 - 09:08 PM, said:

why wouldn't it?


A few reasons, actually. First, typedef is basically an alias to some other definition, even an anonymous one. So, the first fail is:
typedef struct a { /* .. */ };  // <-- and the name of the typedef is ????.



Now, while a struct in struct is fine. a typedef in a struct in not. This would work:
typedef struct a {
	int x;
	struct { int y; } b;
} a;


Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Structure within a structure

Posted 29 April 2010 - 06:26 AM

View Postbaavgai, on 29 April 2010 - 08:55 AM, said:

[quote name='ImaSexy' date='28 April 2010 - 09:08 PM' timestamp='1272510532' post='1004136']
why wouldn't it?
[/quote]

A few reasons, actually.  First, typedef is basically an alias to some other definition, even an anonymous one.  So, the first fail is:
[code]
typedef struct a { /* .. */ };  // <-- and the name of the typedef is ????.



Now, while a struct in struct is fine. a typedef in a struct in not.

Really?

What's wrong with this:
#include <stdio.h>

typedef struct {
  int minutes, hours, days;
  double seconds;
} Clock;

typedef struct {
  Clock c1, c2;
} Timer;

int main() {
  printf("size of Timer: %d\n", sizeof(Timer));
  Timer timer;
  timer.c1.minutes = 5;
  timer.c1.hours = 2;
  timer.c1.days = 3;
  timer.c1.seconds = 44.5;
  printf("total elapsed time is %f seconds\n",
    timer.c1.seconds + 60*timer.c1.minutes + 3600*timer.c1.hours
       + 86400*timer.c1.days);
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#11 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Structure within a structure

Posted 29 April 2010 - 07:02 AM

View Postr.stiltskin, on 29 April 2010 - 07:26 AM, said:

View Postbaavgai, on 29 April 2010 - 08:55 AM, said:

Now, while a struct in struct is fine. a typedef in a struct in not.

Really?

What's wrong with this:
#include <stdio.h>

typedef struct {
  int minutes, hours, days;
  double seconds;
} Clock;

typedef struct {
  Clock c1, c2;
} Timer;




I don't see the word "typedef" inside the block for struct. So what's wrong is that this has little to do with what you're quoting.

To reiterate, this is fine:
typedef struct {
	struct Clock {
		int minutes, hours, days;
		double seconds;
	} c1, c2;
} Timer;



This is not:
typedef struct {
	typedef struct {
		int minutes, hours, days;
		double seconds;
	} Clock;
	Clock c1, c2;
} Timer;



Also, this is not:
typedef struct {
	struct {
		int minutes, hours, days;
		double seconds;
	} Clock;
	Clock c1, c2;
} Timer;



To be clear, defining a named type inside a struct is illegal in C. You may define anonymous types and, of course, you may reference predefined types. This differs from C++, where you can define a type in a type.
Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Structure within a structure

Posted 29 April 2010 - 07:10 AM

OK, sorry, I assumed (who knows why) that we were talking about the OP's question, which has to do with instantiating a struct within a struct, not defining or typedef'ing a struct inside another struct.
Was This Post Helpful? 0
  • +
  • -

#13 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Structure within a structure

Posted 29 April 2010 - 07:19 AM

I assumed I was responding to the quote I had referenced. :P

Still, good for clarification. If one person is confused, others are likely confused as well.
Was This Post Helpful? 0
  • +
  • -

#14 Guest_Andy*


Reputation:

Re: Structure within a structure

Posted 29 April 2010 - 05:44 PM

So my assignment for class was to create a structure within a structure and this was the code my professor gave us. This program needs to add two yards, feet, and inches together and have a total in yards. Then a second part is to use add on to the program to find the area of a rectangle.

I have tried everything and this code doesn't even work as it is

 #include <stdio.h>		

typedef struct
{
	int yards, feet;
	float inches, total;

} yardsfeetincheslength;

typedef struct
{
	int yards, feet;
	float inches, total;

} yardsfeetincheswidth;

typedef struct
{
	yardsfeetincheslength;
	yardsfeetincheswidth;

} yardsfeetinches;

int main()
{
	yardsfeetinches d1, d2, d3, lw, total;

	printf("Please enter the two distances in inches, feet, and yards: \n");

	scanf("%f%d%d%f%d%d", &d1.inches, &d1.feet, &d1.yards, &d2.inches, &d2.feet, &d2.yards);
	
	d1.total = (d1.inches/12.0) + (d1.feet/3.0) + d1.yards;
	d2.total = (d2.inches/12.0) + (d2.feet/3.0) + d2.yards;
	d3.total = d1.total + d2.total;
	lw.total = d1.total * d2.total;

printf ("%.2f Total Yards One + %.2f Total Yards Two = %.2f Yards", d1.total, d2.total, d3.total);
printf ("\nThe length of %.2f times the width of %.2f equals a Area of %.2f ", d1.total, d2.total, lw.total);

}


Was This Post Helpful? 0

#15 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Structure within a structure

Posted 29 April 2010 - 05:53 PM

Andy: Topics merged, there was no need to open a 2nd thread
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2