Wednesday, 15 April 2020

Nested structures in C


When a structure contains another structure as its member, it is called a nested structure.
Syntax:
  struct innerStruct{
     -------------------
     -------------------
};

struct outerStruct{
     -------------------
     -------------------
   struct innerStruct inner;
};

For example, we want to store information about an employee-empName, empNo, dept,address. Address can be broken into houseNo,city,pincode so we will store address details in another structure named Address and other information about employee in Employee structure.
#include<stdio.h>
//Inner structure
struct Address{
  char houseNo[20];
  char city[20];
};

//Outer structure
struct Employee{
  char empName[25];
  char  dept[15];
  struct Address add;
};

int main(){
  struct Employee emp;
  printf("Enter Employee Name: ");
  scanf("%s",&emp.empName);
  printf("Enter Employee Department: ");
  scanf("%s",&emp.dept);

  printf("Enter Employee Address details:\n ");
  printf("Enter Employee House No: ");
  scanf("%s",&emp.add.houseNo);
  printf("Enter Employee City: ");
  scanf("%s",&emp.add.city);

  printf("\nEmployee Details\n");
  printf("Name :%s\n",&emp.empName);
  printf("Department :%s\n",&emp.dept);
  printf("House No :%s\n",&emp.add.houseNo);
  printf("City :%s\n",&emp.add.city);
  return 0;
}

Output:


Note that inner structure must be declared before outer structure.

You may also like:

 Structure in C
 Typedef in C with examples
 Array of Structures in C with Example
 Self Referential Structures in C
 Passing structure to a function in C with example

Please comment and share if you find it useful.


Monday, 13 April 2020

Dynamic memory allocation in C language with example


Dynamic memory allocation in C is performed using one of the standard library functions
malloc, calloc or realloc. Dynamically allocated memory must eventually be freed by calling free. If allocated memory is not properly freed a memory leak results, which could result in a program malfunction.
All these functions are in header file <stdlib.h>.

The malloc() function
This function allocates specified number of bytes.
It takes a single argument.
It does not initialize the allocated memory.
It returns a pointer of type void which can be cast into a pointer of any form.
If space is insufficient, allocation fails and returns a NULL pointer.
Syntax:
    ptr=(cast_type *) malloc(sizeof(type))

Example:
   int *ptr;
   ptr=(int *) malloc(sizeof(int));

   ptr points to memory allotted by malloc() function.

The calloc() function
This function allocates specified number of bytes.
It takes two arguments.
It initializes each block allocated memory with a default value of 0.
It returns a pointer of type void which can be cast into a pointer of any form.
If space is insufficient, allocation fails and returns a NULL pointer.
Syntax:
    ptr=(cast_type *) calloc(n,sizeof(type))
   where
      n= no of elements
      sizeof(type) return no of bytes to be allotted

Example:
   int *ptr;
   ptr=(int *) calloc(n,sizeof(int));

ptr points to memory allotted by calloc() function.


The realloc() function
If someone wants to modify the memory allotted by malloc() and calloc() functions, this function can be used to do this.
Syntax:
  ptr=(cast_type*) realloc(ptr,sizeof(type))

Example:
   int *ptr;
   ptr=(int *) malloc(sizeof(int));

   ptr=(int*) realloc(ptr,newSize);

The free() function
Memory allotted using malloc() and calloc() functions should be freed. It helps to reduce wastage of memory.
Syntax:
  free(pointer_variable);

Example:
   int *ptr;
   ptr=(int *) malloc(sizeof(int));

  free(ptr);


You may also like:
 Structure in C
 Typedef in C with examples
 Array of Structures in C with Example
 Self Referential Structures in C
 Passing structure to a function in C with example

Please comment and share if you find it useful.

Monday, 6 April 2020

Passing Structure to a Function in C with Example


Like other variables, we can also pass individual members of a structure and whole structure also ( structure variable).
First, let us see how to pass individual members of a structure.
Program 1 : Passing individual members of a structure to a function

#include<stdio.h>
//structure declaration
struct student{
   int rollno;
   float marks;
};


int display(int roll, float mrk){
  printf("\nRoll no : %d\tMarks : %.2f\n\n",roll,mrk);
}

int main(){
  struct student s;
  s.rollno=123;
  s.marks=95.5;
  display(s.rollno,s.marks);
  return 0;
}

Output:
Roll no : 123  Marks : 95.50

Program 2 : Passing structure variable to a function : Call by Value
#include<stdio.h>
//structure declaration
struct student{
   int rollno;
   float marks;
};


int display(struct student ss){
  printf("\nRoll no : %d\tMarks : %.2f\n\n",ss.rollno,ss.marks);
}

int main(){
  struct student s;
  s.rollno=123;
  s.marks=95.5;
  display(s);
  return 0;
}

Output:
Roll no : 123  Marks : 95.50

Program 3 : Passing structure variable to a function : Call by Reference
#include<stdio.h>
//structure declaration
struct student{
   int rollno;
   float marks;
};


int display(struct student *ss){
  printf("\nRoll no : %d\tMarks : %.2f\n\n",ss->rollno,ss->marks);
}

int main(){
  struct student s;
  s.rollno=123;
  s.marks=95.5;
  display(&s);
  return 0;
}

Output:
Roll no : 123  Marks : 95.50

Note that when accessing structure members by pointer to a structure, we ->(arrow operator) not dot(.) operator.


You may also like:
 Structure in C
 Typedef in C with examples
 Array of Structures in C with Example
 Self Referential Structures in C

Please comment and share if you find it useful.

Self Referential Structures in C with example

A Self Referencial Structure is a structure which have one or more pointer variables of that structure type as their member(s).

Consider following example:
struct student{
   int rollno;
   float marks;
};

Above structure declaration is standard declaration we studied earlier. But if we add one or more pointer variables of structure student type as the member(s) of student structure, then it will become self referencial structure. See the example below:

struct student{
     int rollno;
     float marks;
     struct student *next;

}

In the above example ‘next’ is a pointer to a structure of type ‘student. Hence, the structure ‘student is a self-referential structure with ‘next as the referencing pointer.




A self referential structure is used to create data structures like linked lists, stacks, queues etc.


You may also like:
 Structure in C
 Typedef in C with examples
 Array of Structures in C with Example

Please comment and share if you find it useful.

  

Sunday, 5 April 2020

Array of Structures in C with example


Like normal arrays of integer, floats etc which have elements of type integer, float respectively, array of structures is nothing but a collection of structures. It means elements of array will be structures not normal type ( integer, float etc.).

Consider following example:

  #include<stdio.h>
   struct student{
       int rollno;
       float marks;
   };

int main(){
   struct student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);

 return 0;
}

In above program, we have declared one structure variable s1 for one student. What if we want to store roll no and marks of 40 students of a class? We have two options either we declare 40 structure variables (s1 to s40) or simply create an array of structures of size 40 as explained in below program:
 
#include<stdio.h>
   struct student{
       int rollno;
       float marks;
   };

int main(){
   struct student s[5];
   int i;

   printf("\nEnter Students Details\n");
   for(i=0;i<5;i++){
       printf("\nEnter Roll No and marks of student %d\n",i+1);
       scanf("%d",&s[i].rollno);
       scanf("%f",&s[i].marks);
   }
  
   printf("\nStudents Details\n");
   for(i=0;i<5;i++){
      printf("\nRoll No and marks of student %d are %d and %f respectively\n",i+1,s[i].rollno,s[i].marks);
   }

 return 0;
}

Output:



You may also like:
 Structure in C
 Typedef in C with examples
 Self Referential Structures in C with Example

Please comment and share if you find it useful.

Friday, 3 April 2020

Typedef in C with examples


Consider following example:
#include<stdio.h>
   struct student{
       int rollno;
       float marks;
   };

int main(){
   struct student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   struct student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

As you can see in above example, every time, a structure variable is being declared, we have to write struct student varName.
To avoid writing struct all over the place, we can use typedef with structure declaration. Using typedef avoids having to write struct every time you declare a variable of that type.
 Example:
#include<stdio.h>
   typedef struct student{
       int rollno;
       float marks;
   };
int main(){
   student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Another version of above program:
  #include<stdio.h>
   typedef struct {
       int rollno;
       float marks;
   }student;
int main(){
   student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Another version of same program:
  #include<stdio.h>
   struct student{
       int rollno;
       float marks;
   };
   typedef struct student;
int main(){
   student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Another version of above program:
  #include<stdio.h>
   struct student{
       int rollno;
       float marks;
   };
   typedef struct student s;
int main(){
   s s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   s s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Summary:
(i). Typedef keyword is used to give a new symbolic name(i.e alias) to another existing type.
(ii). It saves some typing.
(iii). It makes code cleaner.




Please comment and share if you find it useful.