Skip to main content

Posts

Showing posts with the label Structure

C Program using array of structure

/*C Program to accept ‘n’ students information like Rollno, Name, Marks of 4 subjects . Calculate the total and average of marks using structure*/     #include<stdio.h>   struct StudentInfo   { int Rno;   char Sname[20];   int marks[4];   float avg;   }s[10];   void main()   { int i, j, n, total;   printf("\n How many students info you want to enter? "); scanf("%d",&n);   printf("\n Enter student information RNo, Name, Marks of 4 subjects.");  for(i=0;i<n;i++)   {   total=0;   scanf("%d%s",&s[i].Rno, s[i].Sname);   for(j=0;j<4;j++)   { scanf("%d", &s[i].marks[j]);   total=total + s[i].marks[j];   }    s[i].avg=(float)total/4;   }   printf("\nThe student details are:");   for(i=0;i<n;i++)   {    printf(...

Array of Structure

 If we want to store information of many students we need to use array of structure. To store large number of similar records C allows us to create an array of structure variables.      Using array of structure we can easily and efficiently handle large number of records.     All array elements of structure occupy consecutive memory locations.          Example:      struct StudentInfo      { int Rno;      char Sname[20];      int marks[4];      float avg;      };           struct StudentInfo s[10]; // Here ‘s’ is an array of structure which can store 10 students record.            We can also have array within structure, in above example ‘marks’ is an array within structure StudentInfo. Such members can be accessed by using appropriate subscripts.         ...

C Program using structure

/* C program to accept Student Information (Rollno, Name, Percentage) and display same information using structure. */  #include<stdio.h>  struct StudentInfo  { int Rno;  char Sname[20];  float Percentage;  }s1;  void main()  { printf("\n Enter Student Information:");  printf("\n Student Roll number, Name, Percentage ");  scanf("%d",&s1.Rno);  scanf("%s",s1.Sname);  scanf(“%f",&s1.Percentage);  printf("\n Roll Number : %d",s1.Rno);  printf("\n Student Name : %s",s1.Sname);  printf("\n Percentage : %.2f",s1.Percentage);  } A structure can contain an array as its member. In the StudentInfo structure, the structure contains a string which is an array of characters. If we wish to store the marks of 4 subjects of a student, the declaration will be:  struct StudentInfo  { int Rno;  char Sname[20];  int marks[4];  float avg;  }s;  The members will be  s.Rno, s....

Accessing structure members

Accessing structure members There are two different ways to access structure members   1) using dot (.) operator   2) using pointer to structure Accessing  structure members using dot operator In this dot(.) operator is used between structure variable and structure member name.   Example: struct StudentInfo s1;   s1.Rno;    Here ‘s1’ is variable and ‘Rno’ is member of structure StudentInfo. Accessing structure members using pointer   In this -> operator is used between structure pointer variable and member name.   Example: struct StudentInfo *ptr, s1;   ptr=&s1;   ptr->Rno;   Here ‘*ptr’ is a pointer to structure StudentInfo which stores address of structure variable ‘s1’. So now through ‘ptr’ we can access ‘Rno’. Copying structure variable   There are two different ways to copy one structure variable into another.    Copying each structure member i...

Calculation of size of a structure

Size of structure is the addition of individual sizes of each data member of that structure.  We can calculate size of structure by using sizeof operator.    sizeof is an operator which is used to calculate size of any data type or variable.  So here size of structure variable s1 can be calculated as:  sizeof(s1)=sizeof(Rno)+sizeof(Sname)+sizeof(Percentage) sizeof(s1)= 2 + 20 + 4  sizeof(s1)=26 bytes  ( if int occupies 2 bytes) Initialization of a structure variable     Assigning default values to a structure variable at the time of its declaration is called as initialization of structure variable. Values get assigned to the structure data members from first to last so it should match the data type of the data members and it should follow order. Structure data members get initialized using curly brackets ‘{ }’.      If we initialize less number of members remaining members will get initialized to 0 and if we initialize mor...

Creation of a structure variable

There are two different ways to create structure variable  struct StudentInfo  {  int Rno;  char Sname[20];  float Percentage;   }s1, s2;  struct StudentInfo   {   int Rno;   char Sname[20];   float Percentage;    }; use struct StudentInfo s1,s2;  Here s1 and s2 are structure variables of structure StudentInfo.  Creation of a structure variable reserves memory space. Also Read Calculation of size of Structure Accessing Structure Members C program using Array of Structure Array of Structure C program using structure Syntax of Structure

Syntax of structure

struct <struct-name>  {  <data_type> <data_member1>; <data_type> <data_member2>; ...  } ;   Declaration of a structure:    struct StudentInfo   {   int Rno;   char Sname[20];  };    float percentage; Here struct is a keyword,  StudentInfo is a name of structure which is group of elements of different data types like int, string and float. Rno, Sname and Percentage are members of structure StudentInfo enclosed in { }, structure declaration ends with semi-colon (;).   Declaration of structure does not reserve any memory space . Also Read Creation of structure variable Calculation of size of Structure Accessing Structure Members C program using Array of Structure Array of Structure C program using structure