Skip to main content

Posts

Showing posts with the label structure in C programming

Passing structure to a Function

      Like an ordinary variable, a structure variable can also be passed to a function. We  may either pass individual structure elements or the entire structure. Let us take an example in which there is structure employee that has name, sex and salary as members. These members can be passed to function to display their value.    Example:                In this example, member variables are displayed by passing individual elements to a function display(). void display(char name[],char s, int sal)   {    printf("\nName=%s\tSex=%c\tSalary=%d",name,s,sal);   } void main()   {   struct employee      {     char name[20];     char sex;     int salary;    };   struct employee e={"abc",'M',10000};   display(e.name,e.sex,e.salary);                                       /*function call in which each elements are passed individually*/   getch();   } Another Example:         Let us consider above structure but pass who

Structure and Pointer

   To store address of a structure type variable, we can define a structure type pointer variable as normal way. Let us consider an structure book that has members name, page and price. It can be declared as            struct book                  {                     char name[10];                    int page;                    float price;                     }; Then, we can define structure variable and pointer variable of structure type. This can be done as          struct book b, *bptr;        Here,                      b is the structure variable and bptr is pointer variable which points address of structure variable. It can be done as                     bptr=&b;         Here, the base address of b can assigned to bptr pointer. An individual structure member can be accessed in terms of its corresponding pointer variable by writing             ptr_variable->member   Here, -> is called arrow operator and there must be pointer to the

Structure within another structure (Nested Structure) in C programming

     O ne structure can be nested within another structure in C. Let us consider an structure mark which has members subject and marks. This can be nested within another structure student. This can be done as follow.    struct mark{                char subject[20];                float marks;                 }; This structure can be nested within another structure as its member.   struct student {             char name[20];             int roll;             struct mark studentM;             char remark;              } s; Here, structure student contains a member studentM which is itself a structure with two members. The members within structure mark are accessed as                    s.studentM.subject and s.studentM.marks But the members within student structure are accessed as usual          s.name, s.roll and s.remark The above nested structure can be nested in alternative way.              struct student {             char name[20];      

Array of structure in C programming

     Like array of int, float or char type, there may be array of structure. In our previous structure example, if we want to keep record of more students, we can make more structure variables like st1, st2, st3, ….so on. But this technique is inefficient. At this situation, we can use array of structure. The array of structure is declared as            struct  structure_variable[size_of_array];       This is illustrated by following program. Example:         Modify the above program such that it works to keep records of five students. Use array of structure. void main()   {   struct student {                        char name[20];                         int roll;                        float marks;                        char remarks;                         };   struct student s[5];  int i; //array declaration  for(i=0;i<5;i++)     {    printf("\n\n\nEnter information of Student [%d]\n",i);    printf("Enter name:\t");  

Processing a Structure in C programming

                  The members of a structure are usually processed individually, as separate entity. A structure member can be accessed by writing    structure_variable.member              Here, structure_variable refers to the name of  a structure-type variable and member refers to the name of a member within the structure. Again, dot that separates the variable name from the member name.                  In the case of nested structure (if a structure member is itself a structure), the member within inner structure is accessed as                structure_variable.member.submember                                                       This will be clear in nested structure section. An Example:                 Create a structure named student that has name, roll, marks and remarks as members. Assume appropriate types and size of member. Write a program using structure to read and display the data entered by the user. void main()   {     struct student{         

How structure elements are stored?

  The elements of a structure are always stored in contiguous memory locations. This can be illustrated as void main() {     struct student      {      int roll;      float marks;      char remarks;   };   struct student st={200,60.5,'P'};   printf("\nAddress of roll=%u",&st.roll);   printf("\nAddress of marks=%u",&st.marks);   printf("\nAddress of remarks=%u",&st.remarks);   getch(); } Output: Address of roll=65518 Address of marks=65520 Address of remarks=65524                  st.roll               st.marks                          st.remarks 200 60.5 ‘P’               65518             65520                               65524           

Structure in C programming

A structure is a collection of variables usually of different types that can be used as a single item to handle data more efficiently. An array can be used only to represent a group of data items that belong to the same type. But, if we want to represent a collection of data items of different types using a single name, array can not be used. At that situation, structure is used.             Defining a Structure: Syntax:      struct   structure_name                 {                   type1 member_variable1;                  type2 member_variable2;                  .                  .                 typen member_variablen;                }; Once structure_name is declared as new data type, then variables can be declared as          struct structure_name structure_variable; An example:               Let us create a structure named student that has name, roll, marks and remarks as members.     struct student                {                   char