Thursday, November 20, 2014

4.2 STRUCTURES

A structure gathers together, different atoms of information that comprise a given entity.

 

Sample example:

struct book

{

char title[10];

int pages;

float price;

};

 

The general format of a structure definition is as follows:

struct tag_name

{

datatype member1;

datatype member 2;



};

 

In defining, a structure you may note the following syntax:

1.      The template is terminated with a semicolon.

2.      While the entire definition is considered as a statement, each member is declared independently for its name and type in a separate statement inside the template.

 

Difference between array and structures

ARRAY
STRUCTURE
An array is a collection of related data elements of same type.
A structure can have elements of different types.
An array is a derived data type.
A structure is a user-define data type.
An array behaves like a built-in data type i.e. we declare an array variable and use it.
In case of structure, first we have to design and declare a data structure before the variables of that type are declared and used.

 

Declaring Structure Variables

After defining a structure format we can declare variables of that type.

General format
Sample Code
struct tag_name
{
datatype member1;
datatype member 2;
}var1,var2;
 
struct book
{
char title[10];
int pages;
float price;
}b1,b2;
 
struct tag_name
{
datatype member1;
datatype member 2;
};
struct tag_name var1,var2;
 
struct student
{
char name[10];
int rollno;
};
struct student s;
 

 

Accessing Structure members

We can access the members of a structure using a dot(.) operator.

e.g.

strcpy(b1.title,”BASIC”);

b1.pages=360;

b1.price=172.00;

 

How Structure Elements are stored

The elements of a structure are always stored in contiguous memory locations.

Sample code:

struct book

{

char title[10];

int pages;

float price;

};

struct book b1={“BASIC”,360,172.00};

 


Copying of Structures

The values of a structure variable can be assigned to another structure variable of the same type using the assignment operator.

 

/* Program to demonstrate copying of structures */

#include<stdio.h>

#include<conio.h>

void main()

{

  struct employee

  {

     char name[10];

     int age;

     float salary;

  };

  struct employee e1={"Arnav",28,40000.00};

  struct employee e2,e3;

  clrscr();

  /* Copying one by one */

  strcpy(e2.name,e1.name);

  e2.age=e1.age;

  e2.salary=e1.salary;

  /* Copying all elements at one go */

  e3=e2;

  printf("\n%s\t%d\t%f",e1.name,e1.age,e1.salary);

  printf("\n%s\t%d\t%f",e2.name,e2.age,e2.salary);

  printf("\n%s\t%d\t%f",e3.name,e3.age,e3.salary);

  getch();

}

 

NOTE: This copying of all structure elements at one go has been possible only because the structure elements are stored in contiguous memory locations.

 

Nesting of Structures

One structure can be nested within another structure. Using this facility complex data types can be created.

 

/* Program to demonstrate nesting of structures */

#include<stdio.h>

#include<conio.h>

void main()

{

  struct employee

  {

     char name[10];

     struct address

     {

       char phone[10];

       char city[10];

       int pin;

     }a;

  };

  struct employee e={"Arnav","6234560016","Dallas",4210};

  clrscr();

  printf("\nName-->%s\nPhone-->%s",e.name,e.a.phone);

  printf("\nCity-->%s\nPincode-->%d",e.a.city,e.a.pin);

  getch();

}

 

Array of Structures

An array is a collection of similar data types. Similarly, we can also define an array of structures. This means that the structure variable would be an array of objects, each of which contains the member elements declared within the structure construct.

 

/* Program to demonstrate array of structures */

#include<stdio.h>

#include<conio.h>

void main()

{

  struct marks

  {

    int sub1;

    int sub2;

    int sub3;

  }s[5];

  int i;

  clrscr();

  printf("Enter the marks of students:\n");

  for(i=0;i<5;i++)

  {

    printf("\nEnter marks of Student-%d:",i+1);

    scanf("%d%d%d",&s[i].sub1,&s[i].sub2,&s[i].sub3);

  }

  printf("\n\n Marks of students:\n");

  for(i=0;i<5;i++)

  {

    printf("\nStudent-%d:",i+1);

    printf("%d\t%d\t%d",s[i].sub1,s[i].sub2,s[i].sub3);

  }

  getch();

}

 

Passing Structures as arguments in Functions

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 variable at one go.

 

/* Program to demonstrate passing structures to functions */

#include<stdio.h>

#include<conio.h>

#include<string.h>

  struct record

  {

    char name[10];

    int rollno;

    float percent;

  }r;

void display(struct record r);

void main()

{

  clrscr();

  strcpy(r.name,"Arnav");

  r.rollno=20;

  r.percent=92.0;

  display(r); 

  getch();

}

void display(struct record r)

{

  printf("\nSTUDENT RECORD");

  printf("\nName:");

  puts(r.name);

  printf("Roll#:%d",r.rollno);

  printf("\nPercentage:%f",r.percent);

}

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.