Friday 15 October 2010

Structures

Defining a Structure
struct keyword is used to define a structure. Keyword struct define a derived data type which is a collection of different type of data.

Syntax :
struct structure_name
{
 //Declaration of variables
};

Example of Structure
struct Book
{
 char name[15];
 int price;
 int pages;
};
Here the struct Book declares a structure to hold the details of book which consists of three data fields, namely name, price and pages. These fields are called structure elements or members. Each member can have different data type,like in this case, name is of char type and price is of int type etc. Book is the name of the structure and is called structure tag.

Declaring Structure Variables

It is possible to declare variables of a structure, after the structure is defined. Structure variable declaration is similar to the declaration of variables of any other data types.

Structure variables can be declared in following two ways.

1) Declaring Structure variables separately
struct Employee
{
 char[20] name;
 int age;
 int id;
} ;
struct Employee S1 , S2;   //declaring variables of Student

2) Declaring Structure Variables with Structure definition
struct Employee
{
 char[20] name;
 int age;
 int id;
} S1, S2 ;

Here S1 and S2 are variables of structure Employee. However this approach is not much recommended.
Accessing Structure Members Structure members can be accessed and assigned values in number of ways. Structure member has no meaning independently.
In order to assign a value to a structure member, the member name must be linked with the structure variable using dot . operator also called period or member access operator.

struct Book
{
 char name[15];
 int price;
 int pages;
} b1 , b2 ;

b1.price=200;      //b1 is variable of Book type and price is member of Book

We can also use scanf() to give values to structure members through console.

scanf(" %s ", b1.name);
scanf(" %d ", &b1.price);

Structure Initialization
Like any other data type, structure variable can also be initialized at compile time.

struct Person
{
 float height;
 int weight; 
 int age;
};

struct Person p1 = { 180.75 , 73, 23 };    //initialization
or
struct Person p1;
p1.height = 180.75;     //initialization of each member separately
p1.weight = 73;
p1.age = 23;

#include<stdio.h>
#include<conio.h>
struct employee
{
 char ename[10];
 int sal;
};

struct employee emp[5];
int i,j;
void ask()
{
 for(i=0;i<3;i++)
 {
  printf("\nEnter %dst employee record\n",i+1);
  printf("\nEmployee name\t");
  scanf("%s",emp[i].ename);
  printf("\nEnter employee salary\t");
  scanf("%d",&emp[i].sal);
 }
 printf("\nDisplaying Employee record\n");
 for(i=0;i<3;i++)
 {
  printf("\nEmployee name is %s",emp[i].ename);
  printf("\nSlary is %d",emp[i].sal);
 }
}
void main()
{
 clrscr();
 ask();
 getch();
}


#include <stdio.h>
struct student{
    char name[50];
    int roll;
    float marks;
};
int main(){
    struct student s;
    printf("Enter information of students:\n\n");
    printf("Enter name: ");
    scanf("%s",s.name);
    printf("Enter roll number: ");
    scanf("%d",&s.roll);
    printf("Enter marks: ");
    scanf("%f",&s.marks);
    printf("\nDisplaying Information\n");
    printf("Name: %s\n",s.name);
    printf("Roll: %d\n",s.roll);
    printf("Marks: %.2f\n",s.marks);
    return 0;
}

//Dynamic Structure

#include <stdio.h>
#include<stdlib.h>
struct name {
   int a;
   char c[30];
};
int main(){
   struct name *ptr;
   int i,n;
   printf("Enter n: ");
   scanf("%d",&n);

/* Allocates the memory for n structures with pointer ptr pointing to the base address. */
   ptr=(struct name*)malloc(n*sizeof(struct name));
   for(i=0;i<n;++i){
       printf("Enter string and integer respectively:\n");
       scanf("%s%d",&(ptr+i)->c, &(ptr+i)->a);
   }
   printf("Displaying Infromation:\n");
   for(i=0;i<n;++i)
       printf("%s\t%d\t\n",(ptr+i)->c,(ptr+i)->a);
   return 0;
}

//n structures
#include <stdio.h>
#include<stdlib.h>
struct name {
   int a;
   float b;
   char c[30];
};
int main(){
   struct name *ptr;
   int i,n;
   printf("Enter n: ");
   scanf("%d",&n);
   ptr=(struct name*)malloc(n*sizeof(struct name));
/* Above statement allocates the memory for n structures with pointer ptr pointing to base address */
   for(i=0;i<n;++i){
       printf("Enter string, integer and floating number  respectively:\n");
       scanf("%s%d%f",&(ptr+i)->c,&(ptr+i)->a,&(ptr+i)->b);
   }
   printf("Displaying Infromation:\n");
   for(i=0;i<n;++i)
       printf("%s\t%d\t%.2f\n",(ptr+i)->c,(ptr+i)->a,(ptr+i)->b);
   return 0;

You may like the following posts:

          Bit fields
User Defined Data types:
         Enum or Enumerations

No comments:

Post a Comment