Tuesday, November 16, 2010

Structure in C Programming Language

In this page we will learn basic ideas on structure. Structure is the junction from where one can proceed towards object oriented programming.

Need of structure


We have seen that an array can hold a number of values or elements, but all must be of the same type. A structure is a collection of simple variables but the variables may be of different types. Some can be int; some can be float and so on.

A simple C Program on structure

 Let’s start a simple structure that contains three variables, two of int type and one of float type. This structure represents an item in an automobile company’s spare parts inventory. We can define a structure to hold this information as follows:

struct part
{
int model, partno;
float cost;
};

The keyword struct declares a structure a hold the details of the three fields – model, partno and cost. These fields are called structure members or elements. Each member can be of different types. part is the name of the structure and it is known as structure tag. Within the opening and the closing braces of the structure all the structure members should be declared. A semicolon follows the closing brace, terminating the entire structure. It simply describes a format called template to store information. The keywords struct serves as a blueprint for the creation of variables of type part.

Pointers, arrays and other structures can be included as elements within a structure.
The complete program would look like:

#include< stdio.h >
struct part
{
 int model, partno;
 float cost;
 };
 void main ()
 {
   struct part part1;
  clrscr ();

  part1.model=1111;
  part1.partno=2222;
  part1.cost=33.50;
  printf ("Model:-%d", part1.model);
  printf ("\nPart number :-%d", part1.partno);
  printf ("\nCost of the part Rs.:-%.2f", part1.cost);
  getch ();
  }

The first statement within the void main () is struct part part1 declares a variable called part1of type struct part. This statement reserves space in memory for part1.
Once the variable of the structure is created its members can be accessed using the dot (. ) operator.

Again the previous program on structure can be written as:

#include< stdio.h >
struct part
{
 int model, partno;
 float cost;
 };
 void main ()
 {
   struct part part1={10,20,30.25};
  clrscr ();
  printf ("Model:-%d", part1.model);
  printf ("\nPart number :-%d", part1.partno);
  printf ("\nCost of the part Rs.:-%.2f", part1.cost);
  getch ();
  }

Dynamic initialization of structure variables

At the time of declaring the variable of the structure we can assign values.
Our next program will create two structure variables and the value of the members of the first variable will be assigned to the members of the second variable.

#include< stdio.h >
struct part
{
 int model, partno;
 float cost;
 };
 void main ()
 {
   struct part part1={10,20,30.25};
   struct part part2;
  clrscr ();
  printf ("Model (1st):-%d", part1.model);
  printf ("\nPart number (1st) :-%d", part1.partno);
  printf ("\nCost of the part (1st) Rs.:-%.2f", part1.cost);
  part2=part1;
  printf ("\nModel (2nd):-%d", part2.model);
  printf ("\nPart number (2nd) :-%d", part2.partno);
  printf ("\nCost of the part (2nd) Rs.:-%.2f", part2.cost);
  getch ();
  }

2 comments:

Subscribe via email

Enter your email address:

Delivered by FeedBurner