by Dinesh Thakur Category: C Programming (Pratical)

 A structureincludes manyvariablesinto one.Works asa Report cardthat hasname, marks and total.Therecordwould bea structure.The structurethenis used to groupa set ofsimilar datanotforming anew data type.

Tocreate a structurewe use thestructcommand.Its general formis:

struct student
{
    char name[10];
    int m[3];
    int total;
    char result[5];
}p,s;

Thestudent is the name forthe structure. Thep and sare optional andwould bevariable namesthatyou'd be declaring andthat would be ofstudent type.A first example:

# include <string.h>
# include <stdio.h>
struct student
{
        char name[10];
        int m[3];
        int total;
        char result[5];
}*p,*s;
        void main()
    {
              int i,j,l,n;
              clrscr();
              printf("Enter the no. of students : ");
              scanf("%d",&n);
              p=(struct student*)malloc(n*sizeof(struct student));
              s=p;
              for(i=0;i<n;i++)
                  {
                             printf("Enter a name : ");
                             scanf("%s",&p->name);
                             p-> total=0;l=0;
                             for(j=0;j<3;j++)
                                {
                                     one:printf("Enter Marks of %d Subject : ",j+1);
                                     scanf("%d",&p->m[j]);
                                     if((p->m[j])>100)
                                        {
                                            printf("Wrong Value Entered");
                                            goto one;
                                        }
                                            p->total+=p->m[j];
                                            if(p->m[j]<40)
                                                l=1;
                                  }
                                            if(l==0)
                                              strcpy(p->result,"PASS");
                                            else
                                              strcpy(p->result,"FAIL");
                                              p++;
                      }
                                              for(i=0;i<n;i++)
                                                  {
                                                        printf("\n%s\t%s",s->name,s->result);
                                                        s++;
                                                  }
                                                        getch();
       } 

Structure of N Students using Dynamic Memory Allocation

Related Articles Of C Programming






About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.