Structure programming practice

Thread Starter

anukalp

Joined Jul 28, 2018
158
I think this program is best to store details of students

C:
/*
Program to store record of student using structure
Author   : Anukalp
Language : C Programming
Date     : 24.8.2018
*/

/* Header files */
#include<stdio.h>
#include<string.h>

struct Student
{
    int roll_number;    /* Decleare variable to store integer value */
    char name[50];      /* Decleare variable to store 50 character */
    char branch[50];      /* Decleare variable to store 50 character */
    float parcentage;   /* Decleare variable to store float value */
} record[100];

/* Program start from here */
int main (void)

{
    int i;

     printf ("students information \n");
  
    // storing information
    for ( i = 0; i < 100; ++i)
    {
        record[i].roll_number = i+1;

        printf ("\nEnter information For Roll Number %d,\n",record[i].roll_number);

        printf ("Enter Name: ");
        scanf ("%s",record[i].name);

        printf ("Enter Branch: ");
        scanf ("%s",record[i].branch);
    
        printf ("Enter parcentage : ");
        scanf ("%f",&record[i].parcentage);

        printf ("\n");
    }
     return 0; //Program complete successful
}
 

dl324

Joined Mar 30, 2015
18,329
I think this program is best to store details of students
You should add a way for the loop to be terminated early if there aren't 100 students.

Instead of taking my advice on using a pointer to char for the string variable and using malloc() to allocate an appropriate amount of memory at runtime, you simply increased the fixed size and added another string variable that increases potential waste.

sscanf() has the same liability as strcpy() when the destination isn't large enough for the string and its terminating null character.

There is no reason for the record array to be global.

You ask for advice; yet you seem unwilling to take it. Are you trolling AAC?
 
Last edited:

Thread Starter

anukalp

Joined Jul 28, 2018
158
Instead of taking my advice on using a pointer to char for the string variable and using malloc() to allocate an appropriate amount of memory at runtime, you simply increased the fixed size and added another string variable that increases potential waste.
I've just postponed your idea for now because I do not know have much understanding of dynamic memory allocation. I felt I should study it well and then use it in code later.

You ask for advice; yet you seem unwilling to take it. Are you trolling AAC?
I'm trying to make program as you have given advice But i will take time to make it because of the lack of programming knowledge
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Instead of taking my advice on using a pointer to char for the string variable and using malloc() to allocate an appropriate amount of memory at runtime, you simply increased the fixed size and added another string variable that increases potential waste.
Is it better then previous program
C:
/* Header files */
#include<stdio.h>
#include<stdlib.h>

struct student
{
    int roll_number; 
    char name[50];   
    char branch[50];    
    float parcentage; 
};
/* Program start from here */
int main (void)
{
     int i, record;
     struct student *ptr;
   
     printf ("Students information \n");
     printf("\nEnter number of records: ");
     scanf("%d", &record);

     ptr = (struct student *) malloc (record * sizeof(struct student));
   
    // storing information
    for ( i = 0; i < record; i++)
    {
        printf ("\nEnter information For Roll Number : ");
        scanf("%d",&(ptr+i)->roll_number);
      
        printf ("Enter Name: ");
        scanf("%s",&(ptr+i)->name);
      
        printf ("Enter Branch: ");
        scanf("%s",&(ptr+i)->branch);
      
        printf ("Enter parcentage : ");
        scanf("%f",&(ptr+i)->parcentage);
        printf ("\n");
    }
     return 0; //Program complete successful
}
 

dl324

Joined Mar 30, 2015
18,329
Is it better then previous program
Yes, it's better. Could still be made better.

What do you plan to do with the data after it has been entered? If you want to do more than print it in the order it was entered, say alphabetize by name, access will be cumbersome.

If you created an array of pointers to struct student, sorting by any field would be more straightforward, IMO.

Rather than allocate fixed length strings, I'd make a buffer that is large enough for the largest string you intend to read, check the length of each string read, and malloc sufficient space for the string that was read (plus it's terminating null character), then copy the string.
 

WBahn

Joined Mar 31, 2012
32,847
If you malloc() memory for the string in each record (which is the "proper" way to do things), then it is very important that you remember to free up that memory each time you are done with a record. Freeing the memory for the record itself is not sufficient as this will NOT free the memory for the string. The result is a memory leak. Using functions to create, manipulate, and destroy structures is arguably the best way to deal with this.
 
Top