-->

Facebook

Jasper Roberts - Blog

Wednesday, April 15, 2015

Pointers and Structures

For representing complex data structures, we can use structures and pointers together.

Pointer can be a member of structure, or a pointer to a structure, or an array of pointers to structure.

We can declare a pointer to structure as we do for pointers to basic data types.

Structure Syntax:
            struct structure_name
            {
                        datatype-1 variable-1;
                        datatype-2 variable-2;
                                    .
                                    .
                        datatype-n variable-n;
            }struct_variable, ponter-to-struct_variable;

Here, struct is a keyword which is used to define a structure template.
struct name is a name of the structure. It is user defined name.
The members of the structure are written in curly { } braces.
The structure is terminated by semicolon (;).

Structure to Pointer Example:
            struct student
            {
                        int rollno;
                        char name[50];
                        char branch[20];
                        float per;
            }s1, *sptr;

sptr = &s1;

Now, sptr points to s1 and can be used to access member variable of struct student.

To access member variables using pointer to structure, we have to use an arrow -> operator.
Syntax:
            struct_pointer -> member_variable;
Here, struct_pointer is a pointer to structure, and member_variable is the name of member variable.


Example:
            s1 -> rollno = 20;

We can also use dot (.) operator to access member variable with pointer to structure like:
            (*s1).rollno = 20;


Difference between Structure and Union

Structure
Union
1. The keyword  struct is used to define a structure
1. The keyword  union is used to define a structure
2. In structure, each member has its own separate space.
2. In union, all members share the storage space.
3. Memory occupied is the total required to store all the members.
3. Memory occupied is only that much which is required to store the largest member of union.
4. All members can be accessed at anytime.                                                      
4. Only one member can be activated at a time.
5. Syntaxt:
struct strcture_name
      {
         datatype-1 variable-1;
         datatype-2 variable-2;
         datatype-3 variable-3;
           }

5. Syntaxt:
union strcture_name
      {
         datatype-1 variable-1;
         datatype-2 variable-2;
         datatype-3 variable-3;
    }
6. Example:
struct std
      {
          int rollno;
          char name[20];
          float salary;
      }
Total Size required  = 2 + 20+ 4
                                 = 26
6. Example:
union std
      {
          int rollno;
          char name[20];
          float salary;
      }
Total Size required  = max( 2, 20, 4)
                                 = 20

Array of Structure

As we have an array of basic data types, same way we can have an array variable of structure.

Suppose we want to store details of 50 students instead of declaring 50 different variables of student type, we can declare an array of size 50 to store details of 50 students.

Example:
            struct student
            {
                        int id;
                        char name[30];
                        float per;
            };

            struct student s[50];


Program:

A C Program to enter the records of 5 students using structure.

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

struct stdinfo
{
            int rn;
            float per;
};

void main()
{
            struct stdinfo s[3];
            int i;
            clrscr();
                       
            for(i=1;i<=3;i++)
            {
                        printf("\n Enter the student roll no.:");    
                        scanf("%d",&s[i].rn);
                        printf("\n Enter the student percentage.:");         
                        scanf("%d",&s[i].per);
            }

for(i=0;i<=5;i++)
{
            printf("\n Roll Number \t\t Percentage");
            printf("\n %d \t\t %f",s[i].rn,s[i].per);
}
getch();
}         

OUTPUT:
Enter the student roll no.: 1
Enter the student percentage.: 67
Enter the student roll no.: 2
Enter the student percentage.: 76
Enter the student roll no.: 3
Enter the student percentage.: 88

Roll Number        Percentage
       1                      67
       2                      76
       3                      88 

Structure

Structure is a collection of logically related data items of different data types grouped together and known by a single name.
Structure is similar to records in DBMS.
As record consists of fields, the data items of a structure are called as its members.
Syntax:
            struct structure_name
            {
                        datatype-1 variable-1;
                        datatype-2 variable-2;
                                    .
                                    .
                        datatype-n variable-n;
            };

Here, struct is a keyword which is used to define a structure template.
struct name is a name of the structure. It is user defined name.
The members of the structure are4 written in curly { } braces.
The structure is terminated by semicolon (;).

Example:
            struct student
            {
                        int rollno;
                        char name[50];
                        char branch[20];
                        float per;
            }



Accessing the structure members:

We can access the individual members of a structure using the dot (.) operator.
The dot (.) operator is called as structure member operator because it connects the structure variable and its member’s variable.

Syntax:
            struct_variable.struct_member;
Where, struct_var is a variable of structure type, while struct_member is a name of a member variable of structure.
Example:
            struct student s1;
            s1.rollno = 1;
           

Structure Example:

A C Program to input and print following details of student using structure. (roll number, name, address, city, age, per)

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

struct student
{
            int rn;
            char name[20];
            char address[50];
            char city[30];
            int age;
            float per;
}

void main()
{
            struct student s;
            clrscr();
           
            printf(“Enter the roll number:”);
            scanf(“%d”,&s.rn);
            printf(“\n Enter the name:”);
            gets(s.name);
            printf(“\n Enter the address:”);
            gets(s.address);
            printf(“\n Enter the city”);
            gets(s.city);
printf(“\n Enter the age:”);
            scanf(“%d”,&s.age);
printf(“\n Enter the percentage:”);
            scanf(“%f”, &s.per);


            printf(“\n Roll-Number\t Name\t Address\t City\t Age\t Percentage”);
            printf(“\n %d\t %s \t %s \t %s \t %d \t %f”);

            getch();
}

OUTPUT:
Enter the roll number: 28
Enter the name: John
Enter the address: Church Road
Enter the city: Miami
Enter the age: 24
Enter the percentage: 88.00

Roll-Number               Name               Address           City                 Age Percentage

28                                John                 Church Road   Miami 24        88.00