C-Tutorial: STRUCTURES

Latest

Showing posts with label STRUCTURES. Show all posts
Showing posts with label STRUCTURES. Show all posts

Thursday, 20 October 2016

STRUCTURES

Thursday, October 20, 2016 0
STRUCTURES


            “Structure is a collection of non-homogeneous / heterogeneous / different data elements that can be grouped together under a common name”.

Structure Declaration and Definition
           
Syntax:           struct Tag
                                    {
                                                Datatype Member1;
                                                Datatype Member2;
                                                            - - -
                                                            - - -
                                                Datatype Membern;
                                    };

Ø  struct is a keyword used to define structure declaration.
Ø  Tag is name of the structure that follows same rules as a valid identifier.
Ø  Members declared inside the structure declaration are called structure elements (or) structure members.
Ø  Structure declaration must be ended with a semicolon.

Example:        struct Book
                        {
                                    char BName[50];
                                    int Pages;
                                    float Price;
                        };

Declaration of the structure does not reserve any storage space.  Memory is allocated only at the time of defining a structure variable. 

            Syntax:           struct Tag varname1, varname2, - - - - - , varnamep;

            Example:        struct Book B1,B2;

Now, the compiler allocates memory for B1 and B2 as:

i.e., Memory is allocated individually for each structure variable as well as individual memory area for each member of the structure.        

We have to create an object of structure to access its members. Object is a variable of type structure. Structure members are accessed using the dot operator(.) between structure's object and structure's member name.

Syntax :       struct struct_name var_name;

Example for creating object & accessing structure members

       #include<stdio.h>

       struct employee
       {
              int Id;
              char Name[25];
              long Salary;
       };

       void main()
       {
              struct employee E;            
                    printf("\nEnter Employee Id : ");
                    scanf("%d", &E.Id);
                    printf("\nEnter Employee Name : ");
                    scanf("%s", &E.Name);
                    printf("\nEnter Employee Salary : ");
                    scanf("%ld", &E.Salary);
                    printf("\n\n Employee Id : %d", E.Id);
                    printf("\n Employee Name : %s", E.Name);
                    printf("\n Employee Salary : %ld", E.Salary);

       }
Read More

Difference Between Structure and Union

Thursday, October 20, 2016 0
Difference Between Structure and Union


STRUCTURE

UNION
Members of structure do not share memory. So A structure need separate memory space for all its members i.e. all the members have unique storage.
A union shares the memory space among its members so no need to allocate memory to all the members. Shared memory space is allocated i.e. equivalent to the size of member having largest memory.
Members of structure can be accessed individually at any time.
At a time, only one member of union can be accessed.
To define Structure, ‘struct’ keyword is used.
To define Union, ‘union’ keyword is used.
All members of structure can be initialized.
Only the first member of Union can be initialized.
Size of the structure is > to the sum of the each member’s size.
Size of union is equivalent to the size of the member having largest size.
struct struct_name
{
structure ele 1;
structure ele 2;
———-
structure ele n;
}struct_variable_name;
union union_name
{
union ele 1;
union ele 2;
———-
union ele n;
}union_variable_name;
Change in the value of one member can not affect the other in structure.
Change in the value of one member can affect the value of other member.

/* Example program to show the difference between structure and union */

#include<stdio.h>

struct demo1
{
            char a;
            int b;
            float c;
            double d;
}d1;

union demo2
{
            char a;
            int b;
            float c;
            double d;
}d2;

main()
{
            clrscr();
            printf("\n Memory Size of Structure = %d Bytes",sizeof(d1));
            printf("\n Memory Size of Union     = %d Bytes",sizeof(d2));
}
Read More

STRUCTURES AND FUNCTIONS

Thursday, October 20, 2016 0
STRUCTURES AND FUNCTIONS (or) PASSING STRUCTRES TO FUNCTIONS



            A structure can also be passed as an argument to a function in three ways.  Those are:

            Case 1:                        Passing individual member of the structure as an argument
            Case 2:                        Passing entire structure as an argument
            Case 3:                        Passing address of the structure as an argument

Case 1:            
  • When an individual member of the structure is passed as an argument to a function, a copy of member value is passed from calling function to the called function.  
  • Here, Member value is copied into the formal argument of the called function. 
  • Then if we made any changes inside the function with the received value, those changes are not recognized by the outside function.  Since, entire changes are effects only on the formal arguments.


/* TO PASS INDIVIDUAL MEMBER OF THE STRUCTURE AS AN ARGUMENT */

#include<stdio.h>
#include<conio.h>

struct cricket
{
            char name[15];
            int matches;
            float avg;
}s={"sachin",200,76.78};

void change(int);

main()
{
            clrscr();
            printf("\nBefore Passing:");
            printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
            change(s.matches);
            printf("\nAfter Passing:");
            printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
}
void change(int x)
{
            x=210;
}


Case 2:           
  • When entire structure is passed as an argument to a function from calling function to the called function, the receiving argument at the called function must be a structure of the same type. 
  • Here, a copy of the entire structure is copied into the formal structure of the called function.
  • Then if we made any changes inside the function with the received argument, those changes are not recognized by the outside function.  Since, entire changes effects only on the formal structure.




/* PROGRAM TO PASS ENTIRE STRUCTURE AS AN ARGUMENT */

#include<stdio.h>
#include<conio.h>
#include<string.h>

struct cricket
{
            char name[15];
            int matches;
            float avg;
}s={"sachin",210,76.78};

void change(struct cricket);

main()
{
            clrscr();
            printf("\nBefore Passing:");
            printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
            change(s);
            printf("\nAfter Passing:");
            printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
}
void change(struct cricket x)
{
            strcpy(x.name,"sehwag");
            x.matches=250;
            x.avg=50.23;
}

Case 3:           
  • When address of the structure is passed as an argument to a function from calling function to the called function, the receiving argument at the called function must be a structure pointer variable of the same structure type.  
  • With this the pointer variable indirectly points to the original structure.
  • Then if made any changes inside the function on structure, those changes are recognized by outside function.  So, that all changes are effects on the original structure.



/* PROGRAM TO PASS ADDRESS OF THE STRUCTURE AS AN ARGUMENT */

#include<stdio.h>
#include<conio.h>
#include<string.h>

struct cricket
{
            char name[15];
            int matches;
            float avg;
}s={"sachin",210,76.78};

void change(struct cricket *);

main()
{
            clrscr();
            printf("\nBefore Passing:");
            printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
            change(&s);
            printf("\nAfter Passing:");
            printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
}
void change(struct cricket *k)
{
            strcpy(k→name,"sehwag");
            k→matches=250;
            k→avg=50.23;

}
Read More

STRUCTURES AND POINTERS

Thursday, October 20, 2016 0
STRUCTURES AND POINTERS


ü  In C, the pointer is a variable that holds the address of another data variable.
ü  A pointer to structure means a pointer variable can hold the address of a structure.
ü  The address of structure variable can be obtained by using the '&' operator.
ü  All structure members inside the structure can be accessed using pointer, by assigning the structure variable address to the pointer.

Syntax :

struct structname
{
datatype  member1;
datatype  member2;
}variable1, *ptrvariable1;    

Here, ptrvariable1 can be assigned to any other structure of the same type, and can be used to access the members of its structure.

EXAMPLE PROGRAM:

In this program, “record1” is normal structure variable and “ptr” is pointer structure variable. As you know, Dot(.) operator is used to access the data using normal structure variable and arrow(->) is used to access data using pointer variable.

#include <stdio.h>
struct student
{
     int id;
     char name[30];
     float percentage;
};
main()
{
     int i;
     struct student record1 = {1, "Raju", 90.5};
     struct student *ptr;

     ptr = &record1;    

         printf("Records of STUDENT1: \n");
         printf("  Id is: %d \n", ptr->id);
         printf("  Name is: %s \n", ptr->name);
         printf("  Percentage is: %f \n\n", ptr->percentage);
         getch();

}
Read More

NESTED STRUCTURES (OR) STRUCTURE WITHIN STRUCTURE

Thursday, October 20, 2016 0
NESTED STRUCTURES (OR) STRUCTURE WITHIN STRUCTURE



            When a structure is declared as the member of another structure, such representation is known as nested structures (or) structure within structure.  The general format of nested structures is:
Syntax:             

              struct structure1
              {
                     - - - - - - - - - -
                     - - - - - - - - - -
              };

              struct structure2
              {
                     - - - - - - - - - -
                     - - - - - - - - - -
                     struct structure1 obj;
              };


/*  Example for structure within structure or nested structure  */
       #include<stdio.h>

       struct Address
       {
              char HouseNo[25];
              char City[25];
              char PinCode[25];
       };

       struct Employee
       {
           int Id;
           char Name[25];
           float Salary;
           struct Address Add;
       };

       void main()
       {
              int i;
              struct Employee E;

              printf("\n\tEnter Employee Id : ");
              scanf("%d",&E.Id);

              printf("\n\tEnter Employee Name : ");
              scanf("%s",&E.Name);

              printf("\n\tEnter Employee Salary : ");
              scanf("%f",&E.Salary);

              printf("\n\tEnter Employee House No : ");
              scanf("%s",&E.Add.HouseNo);

              printf("\n\tEnter Employee City : ");
              scanf("%s",&E.Add.City);

              printf("\n\tEnter Employee House No : ");
              scanf("%s",&E.Add.PinCode);

              printf("\nDetails of Employees");
              printf("\n\tEmployee Id : %d",E.Id);
              printf("\n\tEmployee Name : %s",E.Name);
              printf("\n\tEmployee Salary : %f",E.Salary);
              printf("\n\tEmployee House No : %s",E.Add.HouseNo);
              printf("\n\tEmployee City : %s",E.Add.City);
              printf("\n\tEmployee House No : %s",E.Add.PinCode);


       }

Read More