Data Structures using C language
Data structures is a set of algorithms, used for organizing data so that it can be used efficiently. There different data structure algorithms for different applications. Stacks, Queues, Linked lists, Trees etc., are important concepts of data structures. To understand Data structures, one should know about structures and pointers. So, lets glance concept of pointers and structures today.
Pointer in a program is a variable that stores the address of another variable with same data type.
Let us understand with an example.
- #include<stdio.h>
- int main()
- {
- int i; // Declaration of variable
- int *p; // Declaration of pointer variable
- i=2; // Initialization of variable i
- printf("\n Value stored in 'i' %d",i); // This printf statement prints 2
- p=&i; // Initialization of pointer
- printf("\n Value stored in 'p' %d",p); // This printf prints address of variable i
- printf(\n Value in 'address' stored in p %d",*p);//This printf prints value of i where p is pointing
- return 0;
- }
Note: Some compilers may give warning at line number 9, because addresses are stored in hexadecimal values in computer memory.
You should remember that,
data type *variable name; is syntax for declaring pointer variable.
pointer variable = &variable; is the syntax for initializing pointer variable.
If 'p' is a pointer variable, " p " gives the value stored in pointer variable, whereas " *p " value stored in memory location where is pointer 'p' is pointing. You can do mathematical and logical operations using pointers (try out your own programs). It must be noted that, program execution time reduces when we use pointers in place of normal variables.
Structures in c is a derived data type, in simple words structure variable is a group of different data types stored in single variable name.(click here for textbook definition of structure).
Let us understand with an example
#include<stdio.h> struct student // Structure variable declaration { int rollNumber; char name[10]; float percentage; }; int main() { struct student data; // variable declaration of 'struct student' type printf("\n Enter student roll number: "); scanf("%d",&data.rollNumber); // Stores roll number entered, in rollNumber of data variable printf("\n Enter student name: "); scanf("%s",data.name); // Stores name entered, in name[10] of data variable printf("\n Enter student percentage: "); scanf("%f",&data.percentage);// Stores roll percentage entered, in percentage of data variable printf("\n Roll number of student: %d\n Name of student: %s\n Percentage of student: %f\n",data.rollNumber,data.name,data.percentage);// prints data stored in respective variables return 0; }
You should remember that,
- struct variable name{ data type variable1;data type variable2;...}; is syntax for declaring a structure
- struct variable structureVariableName; is syntax of declaring a structure variable.
- structureVariableName.variable; is used to refer to variables of structure.
Structure variables are used in arithmetic and logical operations like normal variables.
Structures can also have pointers, as shown below.
- #include<stdio.h>
- struct student // Structure variable declaration
- {
- int rollNumber;
- char name[10];
- float percentage;
- };
- int main()
- {
- struct student *data; // pointer declaration of 'struct student' type
- printf("\n Enter student roll number: ");
- scanf("%d",&data->rollNumber); // Stores roll number entered, in rollNumber of data pointer
- printf("\n Enter student name: ");
- scanf("%s",data->name); // Stores name entered, in name[10] of data pointer
- printf("\n Enter student percentage: ");
- scanf("%f",&data->percentage);// Stores roll percentage entered, in percentage of data pointer
- printf("\n Roll number of student: %d\n Name of student: %s\n Percentage of student: %f\n",data->rollNumber,data->name,data->percentage);// prints data stored in respective variables
- return 0
- }
Note here, that
- struct variable name *structurepointer; is used for declaration of pointer to structure.
- structrepointer->variable is used to refer variables of structure.
Sujay K
Good Content Bro
ReplyDeletesimple and easy :) ty
ReplyDelete