Re: [C] structures

From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 11/28/03


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/


Relevant Pages

  • Re: wrong print
    ... you must be using non-standard libraries. ... int max_line_len = 1024; char **Amm,**Pss; ... char* readline; void scandir; ... Before posting for the first time to a group ALWAYS read the FAQ ...
    (comp.lang.c)
  • Re: Problem 77: Greedy Gift Givers
    ... char name; ... struct friend f; ... int i = 0; ... Then read the rest of the faq also. ...
    (comp.lang.c)
  • Re: update...
    ... Here you assign an "int" value ... into a "char" type. ... > foo test; ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (comp.lang.cpp)
  • Re: simple output input ....
    ... double co2(char fuel, double amount){ ... int main ... char description; ... a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq ...
    (comp.lang.c)
  • Re: help! :(
    ... char name; ... int quizzes; ... int numberStudents, i, j; ...
    (comp.lang.c)