Velocity Reviews > Help on understanding Structs

Help on understanding Structs

Richard Heathfield
Guest
Posts: n/a

 11-18-2005
pete said:

> Suman wrote:
>>
>> 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.

Code is never free;
Constrained by mathematics,
Like a flower's leaves.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Keith Thompson
Guest
Posts: n/a

 11-18-2005
Richard Heathfield <(E-Mail Removed)> writes:
> pete said:
>> Suman wrote:
>>> 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.

>
> Code is never free;
> Constrained by mathematics,
> Like a flower's leaves.

*applause*

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Skarmander
Guest
Posts: n/a

 11-18-2005
Richard Heathfield wrote:
> pete said:
>
>
>>Suman wrote:
>>
>>>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.

>
>
> Code is never free;
> Constrained by mathematics,
> Like a flower's leaves.
>

union possible { short prose; short code; }
peculiar, definitely; long shot, granted;

long objections (double take) {
static short idioms,

interpunction: peculiar; if (nothing) {} else
but: while (peculiar.prose = definitely.code) continue;
}

signed Skarmander;

pete
Guest
Posts: n/a

 11-19-2005
Skarmander wrote:
>
> Richard Heathfield wrote:
> > pete said:
> >
> >
> >>Suman wrote:
> >>
> >>>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.

> >
> >
> > Code is never free;
> > Constrained by mathematics,
> > Like a flower's leaves.

Petals!

--
pete

Bail
Guest
Posts: n/a

 11-19-2005
which C book you suggest i buy?

Richard Heathfield
Guest
Posts: n/a

 11-19-2005
Bail said:

> which C book you suggest i buy?

One of these three:

The C Programming Language, 2nd Ed. Kernighan & Ritchie. Prentice Hall,
1988. ISBN 0-13-110362-8 (paperback), or 0-13-110370-9 (hardback).

C Programming: A Modern Approach, K.N.King, W.W.Norton & Company, 1996.
ISBN 0-393-96945-2

C: How to Program, 2nd Ed. Deitel, H.M. & Deitel, P.J. Prentice Hall, 1994.
ISBN: 0-13-226119-7

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Peter Shaggy Haywood
Guest
Posts: n/a

 11-19-2005
Groovy hepcat Bail was jivin' on 16 Nov 2005 21:02:54 -0800 in
comp.lang.c.
Help on understanding Structs's a cool scene! Dig it!

>I will have a exam on the oncoming friday, my professor told us that it

I realise I'm too late with this followup, but just in case you're

>will base upon this program. i am having troubles understanding this

Well, what don't you understand? It's pretty straight forward,
notwithstanding the numerous inadequecies, which I have outlined. Do I
understand correctly that your professor supplied this code? If so, YE
GADS! What's he a professor of, needlepoint?

>program, for example what if i want to add all the total calories that
>the user input together. determine which food has the largest calories.
>how do i start to modifiy the program inorder to do the things i listed
>above. thanks
>
>#include <stdio.h>
>#include <stdlib.h>
>
>typedef struct {
> char *name;
> int calories;
> int group;
>} FOOD;
>
>void getFood(FOOD foods[ ], int i ) {
> FOOD newFood;
> void *characters = malloc(81*sizeof(char));

sizeof(char) is 1.
You *must* check the return value of malloc() for a null pointer
(indicating an error). malloc() can fail, y'know; and when it does,
you must not try to dereference the pointer.
Why is characters a pointer to void? If you want to store characters
in the memory it points at, then wouldn't a pointer to char be better?

> printf("\nPlease enter the requested information:\n");
> printf(" for food item %d, what is\n", i+1);
> printf(" the name? ");

This prompt may not be displayed straight away. Use fflush() to
flush stdout to better the chances of seeing the prompt.

fflush(stdout);

> gets( characters); // NOTE fgets IS BETTER!

Never use gets(). Just never use gets(). As you (or, presumably your
professor) say in the comment, fgets() is better. Why, then, didn't
you (or he) use fgets()? gets() is a disaster waiting to happen. Never
use it. Use fgets() instead. Never use gets(). Read the FAQ for more
on this; and never use gets(). Just to reiterate: never use gets().
By the way, don't use C++ style comments here. There are (at least)
two good reasons for this. First, many people here still use pre-C99
compilers, which don't support C++ style comments. And second, lines
have a way of wrapping around here. When that happens it makes
cutting, pasting and compiling dificult. Original C comments (/*...*/)
are well behaved when they wrap around. C++ comments are not.

> newFood.name = characters;
>
> printf(" calories/serving? ");

This prompt may not be displayed straight away. Use fflush() to
flush stdout to better the chances of seeing the prompt.

fflush(stdout);

> scanf("%d", &(newFood.calories));
>
> printf(" food group? ");

This prompt may not be displayed straight away. Use fflush() to
flush stdout to better the chances of seeing the prompt.

fflush(stdout);

> scanf("%d", &(newFood.group));
> printf("\n");
> foods[i] = newFood;

You're not going to return some status value to indicate
success/failure conditions?

> } // end getFood // note:a struct is and lvalue !!

What's the point of this useless comment? Comments should make code
clearer, easier to read. They should not just clutter the code with
useless garbage.

>void dump( FOOD foods[ ] , int n ) {
> int i;
> for(i=0; i<n; i++ ) {
> printf("Food %d is: \n", i+1);
> printf(" name : %s\n", foods[i].name);
> printf(" calories: %d\n", foods[i].calories);
> printf(" group # : %d\n", foods[i].group);
> printf("\n");
> } // end for
>} // end dump

>int main( ) {
> int num; // number of foods entered

And another one!

> int i;
> char dum[81];
>
> printf("\nHow many foods will you enter? ");

This prompt may not be displayed straight away. Use fflush() to
flush stdout to better the chances of seeing the prompt.

fflush(stdout);

> scanf("%d", &num);
>
> FOOD foods[num];

Won't work in pre-C99 implementations, where an array's bounds must
be declared by a constant. Instead, you should use malloc() (and do
remember to check the return value).

> for( i=0; i<num; i++ ) {
> gets(dum); //NOTE: THIS IS HERE BECAUSE OF SCANF

Huh? What do you (or your prof) mean? You mean scanf() is leaving a
newline (or other garbage) in the stream to be picked up by the next
input function? Well, then, what does that say about the way you are
using scanf()? What does it say about the whole input strategy? Not
very good, is it? Consult the FAQ on this.
Oh, and by the way, did I mention that you should never use gets()?
Well, you should never use gets().

> getFood( foods, i );
> } // end for
>
> dump( foods, num);

What about relinquishing the memory allocated in getFood()? Very
poor form, leaving this undone!

>return 0;

Poor (inconsistent) indentation! This sort of thing can make code

>} // end main

changing schools. It's horrendous! I'd expect a rank beginner to write
code like that, not a professor (unless he were a professor of
something totally unrelated to programming).
But the logic is pretty simple. Here is a basic breakdown of it.

1) First it asks the user to enter the number of foods he/she wishes
to enter, then reads in that number.

2) Next it creates an array of structures used to hold the food data.

3) Then it goes into a loop that calls a function that
a) asks the user the name of the food and reads in the name, having
first allocated memory in which to store it,
b) asks the user for the number of calories and reads in the
number,
c) asks the user for the food group (a number) and reads in the
number, and
d) stores this information in an element of the array.

4) Lastly it calls a function that displays the information stored in
the array.

--

Dig the even newer still, yet more improved, sig!

"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?