Re: [C] structures
From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 11/28/03
- Next message: B. v Ingen Schenau: "Re: How do I call a function that has name stored in a character"
- Previous message: B. v Ingen Schenau: "Re: Member template instantiation confusion"
- In reply to: amanayin: "Re: [C] structures"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 28 Nov 2003 21:22:53 +0100
<posted & mailed>
amanayin wrote:
> #include <stdio.h>
>
> struct student {
> int student_id;
> char name[15];
I think that 14 characters (remember that 1 additional character is
required for the string terminator '\0') for a name field is too little.
You would be unable to store my full name for example.
I would reserve room for at least 30 usable characters.
char name[31];
As an aside, it is usually not good form to use 'magic' numbers in your
code. If, for example, you want to make the change I discussed above, how
do you know which occurances of the number 15 you need to change?
It is better to create a named constant, and use that. Then the change is
localised to one line in the source, and you always know what quantity to
are referring to.
#define NAME_LENGTH 31 /* This should be near the to of your source */
char name[NAME_LENGHT];
> char grade[15];
> };
>
> struct student stu[4];
> int i;
These two variables should be located inside main().
Having global variables (i.e. outside all functions) is just asking for
trouble in the long run.
>
> int main(void)
good.
> {
>
> for(i = 0; i < 4; i++)
> {
> printf("Enter name, id, grade: ");
> scanf("%s %d %s",stu[i].name, &stu[i].student_id, stu[i].grade);
I have several problems with this method of gathering input.
1. The output of printf() is not guaranteed to be visible until a newline
('\n') has been written or until the output has been explicitly flushed
with fflush(stdout).
2. scanf("%s") has no way of knowing how large your buffer is, so it *will*
overflow the buffer when the user enters too much data.
3. scanf() stops scanning the input when it encounters a white-space
character. This means that it is impossible to read names that contain
spaces.
A better alternative is to read _all_ input with fgets() (or similar
function) and to convert it to the expected format afterwards.
> }
>
> for(i = 0; i < 4; i++)
> {
> printf(" name %s \n sudent id %d\n grade %s\n\n",
> stu[i].name,stu[i].student_id,stu[i].grade);
> }
> return 0;
> }
Here is a version of the program incorporating the changes I mentioned:
(I have not included any additional error checking, so overlong input will
cause some inputs to be skipped and some outputs to be seemingly incorrect.)
#include <stdio.h>
#define NAME_LEN 31
#define GRADE_LEN 5
#define TEMPBUF_LEN 80
#define NUM_STUDENTS 4
struct student {
int student_id;
char name[NAME_LEN];
char grade[GRADE_LEN];
};
int main(void)
{
char tempbuf[TEMPBUF_LEN];
struct student stu[NUM_STUDENTS] = {0};
int i;
for(i = 0; i < NUM_STUDENTS; i++)
{
printf("Enter name, id and grade of studen %d\n", i);
printf("Name : ");
fflush(stdout);
fgets(stu[i].name, NAME_LEN, stdin);
printf("ID : ");
fflush(stdout);
fgets(tempbuf, TEMPBUF_LEN, stdin);
sscanf(tempbuf, "%d", &std[i].sudent_id);
printf("Grade: ");
fflush(stdout);
fgets(stu[i].grade, GRADE_LEN, stdin);
}
for(i = 0; i < NUM_STUDENTS; i++)
{
printf(" name %s \n sudent id %d\n grade %s\n\n",
stu[i].name,stu[i].student_id,stu[i].grade);
}
return 0;
}
Bart v Ingen Schenau
-- a.c.l.l.c-c++ FAQ: http://www.snurse-l.org/acllc-c++/faq.html a.c.l.l.c-c++ FAQ mirror: http://nullptr.merseine.nu:8080/acllcc++.html c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
- Next message: B. v Ingen Schenau: "Re: How do I call a function that has name stored in a character"
- Previous message: B. v Ingen Schenau: "Re: Member template instantiation confusion"
- In reply to: amanayin: "Re: [C] structures"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|