Velocity Reviews > Help on understanding Structs

# Help on understanding Structs

Default User
Guest
Posts: n/a

 11-17-2005
Bail wrote:

> thanks for the info. is there any more ??? help a poor student here

What info?

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded

Bail
Guest
Posts: n/a

 11-18-2005
will someone take the time to explain to me how does the program i
posted above work? and how do i start modifiy the program inorder to
calculate the total calories, max calories. ???

Walter Roberson
Guest
Posts: n/a

 11-18-2005
In article <(E-Mail Removed) .com>,
Bail <(E-Mail Removed)> wrote:
:huh? i dont get where to put this code into my program to make it work?

You would put it in at the point that you wanted to,
"add all the total calories that
the user input together. determine which food has the largest calories."

--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson

Walter Roberson
Guest
Posts: n/a

 11-18-2005
In article <(E-Mail Removed). com>,
Bail <(E-Mail Removed)> wrote:
>will someone take the time to explain to me how does the program i
>posted above work? and how do i start modifiy the program inorder to
>calculate the total calories, max calories. ???

What part of it don't you understand? Do you understand basic
operations with structures? Do you understand the following? If not,
what *specific* lines do you not understand, and what is your best
guess about what those lines are supposed to do?

#include <stdio.h>
typedef struct { int foodno; float foodcalories } FOOD;

void describefood( FOOD thefood ) {
printf( "Food #%d has %f Calories\n", thefood.foodno, thefood.foodcalories );
}

void describefoodptr( FOOD *thefoodptr ) {
printf( "Food #%d has %f Calories\n", thefoodptr->foodno,
thefoodptr->foodcalories );
}

int main(void) {
FOOD firstfood, secondfood;

firstfood.foodno = 1;
firstfood.foodcalories = 1187.3;

secondfood.foodno = 2;
secondfood.foodcalories = 831.88;

describefood( firstfood );
describefoodptr( &secondfood );
}
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson

Bail
Guest
Posts: n/a

 11-18-2005
void describefoodptr( FOOD *thefoodptr ) {
printf( "Food #%d has %f Calories\n", thefoodptr->foodno,
thefoodptr->foodcalories );

}

thefoodptr->foodno???

pete
Guest
Posts: n/a

 11-18-2005
Suman wrote:
>
> pete wrote:

> > If I were to say that he had an abrasive personality,
> > it would not surprise me in the least,
> > if he were to reply with a logical rebuttal
> > of how his personality was not abrasive
> > and how I must be mentally deficient
> > for even thinking something like that.
> >

>
> And I thought I'm the only Ogden Nash fan 'round here.

Most of my English text is in free verse,
and all of my C code is.

--
pete

Ian Malone
Guest
Posts: n/a

 11-18-2005
Bail wrote:

You cut out
>> typedef struct { int foodno; float foodcalories } FOOD;

and other relevant stuff. From a common sig around comp.lang.c atm:
`Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
It's clearer to others what you mean when you quote the bits you're

> void describefoodptr( FOOD *thefoodptr ) {
> printf( "Food #%d has %f Calories\n", thefoodptr->foodno,
> thefoodptr->foodcalories );
>
> }
>
>
> thefoodptr->foodno???
>

FOOD is a typedef for the struct:
typedef struct { int foodno; float foodcalories } FOOD;
An instance of FOOD has members foodno (an integer) and
foodcalories (a float).

If you declared an instance like
FOOD myfood;

You would access those members with:
myfood.foodno
and
myfood.foodcalories

The function you are talking about takes a parameter
(FOOD *thefoodptr)

So thefoodptr is a pointer to an instance of FOOD.

You dereference it to get an instance of food like this:
*thefoodptr /* now an instance of FOOD */
And would get at the members like this:
(*thefoodptr).foodno
and
(*thefoodptr).foodcalories

Except this is an awful syntax, so there is a shorthand
thefoodptr->foodno
The -> means dereference the object to the left (to hopefully
get an instance of a struct) then access the member with the
name on the right.

--
imalone

Flash Gordon
Guest
Posts: n/a

 11-18-2005
Bail wrote:
> will someone take the time to explain to me how does the program i
> posted above work?

There is no program above. Please read one of the over 1000 messages on
this group telling you how to provide context using Google. A search for
the terms "Google context" will find them.

You cannot assume that people have seen previous messages, because they
may not have, nor that they will remember them if they have seen them.

> and how do i start modifiy the program inorder to
> calculate the total calories, max calories. ???

Load it in to an editor is all I can suggest since I don't have the code
you want to modify.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.

Zoran Cutura
Guest
Posts: n/a

 11-18-2005
Bail <(E-Mail Removed)> wrote:
> void describefoodptr( FOOD *thefoodptr ) {
> printf( "Food #%d has %f Calories\n", thefoodptr->foodno,
> thefoodptr->foodcalories );
>
> }
>
>
> thefoodptr->foodno???
>

If you need to learn C you should simply go and do it. You cannot learn
from a newsgroup if you don't get the basics at all.
You'ld better look into your C book (if you don't have one, don't bother
to write the exame at all). If you have certain questions about the
-> operator for example somebody in this ng will surely answer it.

Though you should note, that most are not willing to do your homework.
--
Z ((E-Mail Removed))
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond

Default User
Guest
Posts: n/a

 11-18-2005
Bail wrote:

> will someone take the time to explain to me how does the program i
> posted above work? and how do i start modifiy the program inorder to
> calculate the total calories, max calories. ???

Not until you learn to post correctly. SEE BELOW.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded