Structures in C

Arrays are versatile in C. You can have array of integers, real numbers, pointers etc. But  the elements of the array need to be of same type. What if we need to store dissimilar information together? Say we need to store the name of an employee, along with his employee id and his basic salary?

An array will not help us here. What we need is a structure. A structure is a group of dissimilar data under a common name.

A structure needs to be first declared, mentioning the members of the structure and their data types.

Syntax for structure declaration 


struct tag-name
{
     data-type var-name;
     data-type var-name;
     ---------
};

The keyword for structure is struct. Tag name is an identifier for this structure type and should be used with struct keyword when defining variables.

Members of structures are declared within braces with their data types.

  • Members of structures can be int,char,float,double or string or an array, a pointer or another structure itself. 
  • Members can not be initialized in the structure declaration.
  • It is a good practice to declare a structure globally so that all functions can use it. 

 Example

struct emp
{
   int emp_id;
   char name[40];
   float salary;
};

Defining a structure variable

We should use struct with tag of structure for defining a structure.

struct emp e1;
struct emp earr[10];

If we are using few structures, we can combine structure declaration with variable definition as shown below 

struct point
{
     float x,y;
} p1,p2;

Dot operator

In an array, the ith element is accessed using index operator - square bracket. To access members of a structure, dot operator is used.

p1.x = 34;
e1.emp_id = 1245;
strcpy(e1.name,"Narain");

Let us write a simple program to name,author and price of a book in a structure.


 #include<stdio.h>  
 struct book  
 {  
   int id;  
   char name[100];  
   char author[100];  
   float price;  
 };  
 int main()  
 {  
   struct book b1;  
   printf("Enter name of book");  
   gets(b1.name);  
   printf("Enter author of book");  
   gets(b1.author);  
   printf("Enter price of book");  
   scanf("%f",&b1.price);  
   printf("%s %s %f\n",b1.name,b1.author,b1.price);  
   return 0;  
 }  

 Assigning value to structure

A structure can be assigned to another structure of same type (again unlike an array). This will copy values of members of source struct to corresponding members of destination struct.
 struct book b1,b2 ;  
 strcpy(b1.name,"C programming");  
 strcpy(b1.author,"Ritchie");  
 b1.price = 320;  
 b2 = b1;  

The statement b2 = b1 will copy C programming to b2.name, copy Ritchie b2.author and 320 to b2.price. 

Initialization of structure

A structure can be initialized along with definition. The values for members should be given within braces in correct order. Uninitialized members are set to 0.

struct book bk2={"Learn C","Some author",268};

Functions and structures

A structures can be passed to a function. And unlike an array a structure can be returned from a function. 

Another difference from array is a structure is sent to function using call by value method, i.e. a copy is sent to function. 

Let us look at an example 
 void print_book(struct book b)  
 {  
   printf("Name: %s\n Author :%s\n Price :%f",b.name,b.author,b.price);  
 }  
So a structure is sent to function and it is printed in the function.

But we can not use a similar function to read the book structure. We know the reason. Because the reading will only change the local copy.

If the function need to modify the structure, it should return the structure or it should take structure pointer as parameter.

 struct book read_book()  
 {  
   struct book temp;  
   printf("Name");  
   gets(temp.name);  
   printf("Author");  
   gets(temp.author);  
   printf("Price");  
   scanf("%f",&temp.price);  
   return temp;  
 }  
read_book function reads the elements of a book structure and then returns it to caller.

Pointer to structure 

Taking address of struct gives us a struct pointer. 

struct book * bptr = &b1;

Now to access members of structure using this pointer, we can use arrow operator.

*bptr.price +=10;
m = bptr->price *0.9;

Both *bptr.price and bptr->price access the member price of the structure.  

Functions with structure pointer 

A function can take a structure pointer as a parameter. In fact, for larger structures this method is better as there is no need to copy the structures.

 void read_book(struct book *bptr)  
 {  
   printf("Name");  
   gets(bptr->name);  
   printf("Author");  
   gets(bptr->author);  
   printf("Price");  
   scanf("%f",&bptr.price);   
 }  


Comments

Popular Posts