Structure in C Program.
Hello Everyone,
Today we will discuss about Structure in C Program . Before starting please go through my previous blogs in archive folder for your reference.
dheeraj60.blogspot.com
Let’s Discuss about Structure in C : As we know in Array we define type of variable that can hold several data types of the same types. Whereas Structure is basically used to represent a record, in other word we can say structure is another user defined data type available in C that allows to combined data items of different kinds.
Assume we need to store the data of students like student name, age, address, enrolment number etc. One way of doing this would be creating a different variable for each attribute, however when you need to store the data of multiple students then in that case, we need to create these several variables again for each student. This is bit complex but we can solve this problem easily by using structure. We can create a structure that has members for name, age, address and enrolment number and then we can create the variables of this structure for each student. We can use this by using structure in C with examples for better understanding.
Note : Struct Keyword is use to create structure in C . The struct statement defines a new data type, with more than one member. Defining structure by using struct is as below :
struct [structure tag] {
datatype memberdefinition;
datatype member definition;
...
datatype member definition;
};
Here in above Syntax structure tag is optional and each member definition is a normal variable definition, such as int ,char , float etc.
Understand this by below statement for student attributes in Structure for above syntax:
struct Students {
char name[20];
int age[10];
char address[50];
int enrolment_number;
} students;
To access data members of a structure using a struct variable we use as below :
var_name.member1_name;
var_name.member2_name;
To assign value to structure members we use below method .
1. By using Dot(.) -- var_name.memeber1_name = value;
2. We can assign all members in one statement as below method
struct struct_name var_name =
{value for memeber1, value for memeber2 …}
Let’s take one simple example of Student details by using structure in one Program to understand this in better way.
Program is as below:
#include <stdio.h>
#include <stdlib.h>
struct StudentDetails{
char *student_name;
int age;
char *address;
int enrollment_number;
};
int main()
{
struct StudentDetails student;
student.student_name = "Ashish";
student.age = 21;
student.address = "DELHI";
student.enrollment_number=10;
printf("Student name is: %s", student.student_name);
printf("\nStudent age is: %d", student.age);
printf("\nStudent Address is: %s", student.address);
printf("\nStudent enrollment number is: %d", student.enrollment_number);
return 0;
}
Here in above program we create a structure studentDetails with different members with several data types . and through command struct StudentDetails student; student is the variable of structure StudentDetails and now we can Assigning the values of each struct member here by using student variable and finally through printf function we are displaying the values of struct members in program.
Below is the output of above program in compiler code-block.
Here we can see the output of student details by using structure in C . So through Structure we can define multiple members as variable and can access and display it accordingly in program.
We can use Structures as Function Arguments : We can pass Structure as a function argument in the same way we can pass any variable or pointer . Let understand this with below example/ Program.
#include <stdio.h>
#include <stdlib.h>
struct Students {
char student_name[20];
char address[50];
int age;
int enrollment_number;
};
/* function declaration */
void printstudents( struct Students student );
int main( ) {
struct Students students1; /* Declare students1 of type student */
struct Students students2; /* Declare students2 of type student */
/* students 1 specification */
strcpy( students1.student_name, "Ashish");
strcpy( students1.address, "Delhi Katwaria Sarai");
students1.age = 21;
students1.enrollment_number=100;
/* students 2 specification */
strcpy( students2.student_name, "ABCD");
strcpy( students2.address,"Bangalore");
students2.age = 30;
students2.enrollment_number=101;
/* print students1 info */
printstudent( students1 );
/* Print students2 info */
printstudents( students2 );
return 0;
}
void printstudents( struct Students student ) {
printf( "Student name is : %s\n", student.student_name);
printf( "Student Address is : %s\n", student.address);
printf( "Student Age is : %d\n", student.age);
printf( "Student enrollment number is : %d\n", student.enrollment_number);
}
Here we display the value two times by declaring type of structure in 2 parts as students 1 and students 2 . In code i explained all the syntax in comment section so that you can understand the code properly .Only thing we need to understand strcpy which i not discussed in previous blog
Important :The strcpy() is a function basically used to copies the string pointed by source to the destination. That’s why we used strcpy to both types.
Above Program will Display as below once compiled .
Student Name is : Ashish
Student Address is : Delhi
Student Age is : 21
Student enrolment number is : 100
Student Name is : ABCD
Student Address is : Bangalore
Student Age is : 30
Student enrolment number is : 101
Hope you are able to understand the Structure in C , we can also use Pointer in Structure . Benefits of Structure is that it allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only. Please go through this blog carefully and try to write some different program by using arrays and let me know if you have any doubt . In next Blog i will come up with Pointers uses in Function and C function pointer.
Thanks.
Comments
Post a Comment