Structures in C Programming
Structures are salient features of C programming language and it is a user defined data type that packages integer, float and character data types.
Furthermore, the addresses of data members in a structure are contiguous as you can see in the example.
________________________________
#include<stdio.h>
struct student_record
{
int id_no;
int age;
int enroll_no;
};
int main()
{
struct student_record sr;
int size;
size=sizeof(struct student_record);
printf(“%dn”,size);
printf(“%un”, &(sr.id_no));
printf(“%un”, &(sr.age));
printf(“%un”, &(sr.enroll_no));
return 0;
}
Output of the program you can also see in the video
Size of student_record=12
Address of id_no=1520478752
Address of age=1520478756
Address of enroll_no=1520478760
_____________________________________
Description of the program- The above program declares a structure student record. Which has three
fields all are integers and their names are id_no, age and enroll_no and their size are of four bytes.
Moreover, in main() a variable sr of student type is declared and size of struct student_record is printed which is of 12 bytes. In addition, data members of a structure can be accessed through dot(.) operator.
Furthermore, the members are accessed by variable sr, such as, sr.id_no, sr.age and sr.enroll_no.
Additionally, the members can have values and a simple program we will demonstrate assignment of values to the data members and print them.
_______________________________
#include<stdio.h>
struct student_record
{
int id_no;
int age;
int enroll_no;
};
int main()
{
struct student_record sr;
int size;
printf(“Enter the values of sr.id_no, sr.age and sr.enroll_non”);
scanf(“%dn”, &sr.id_no);
scanf(“%un”, &sr.age);
scanf(“%un”, &sr.enroll_no);
printf(“See entered valuesn”);
printf(“%dn”, sr.id_no);
printf(“%un”, sr.age);
printf(“%un”, sr.enroll_no);
return 0;
}
_________________________________
As primitive variable, the array of structure variable can also be declared in below program we declare an array of structures of size 2 which is sr[2], then there will actually be two variables
sr[0] and sr[1] and they will behave like individual structure variables. Pointer to structure- Pointer to structure variable can also be declared, such as, struct student_record *sr_ptr that can hold the address of the structure variable sr and the members can be accessed through arrow operator(->) such as, sr_ptr->id_no sr_ptr->age and sr_ptr->enroll_no. It would be better to write full program which is.
__________________________________
#include<stdio.h>
struct student_record
{
int id_no;
int age;
int enroll_no;
};
int main()
{
struct student_record sr, *sr_ptr;
sr_ptr= &sr;
int size;
printf(“Enter the values of sr.id_no, sr.age and sr.enroll_non”);
scanf(“%dn”, &sr_ptr->id_no);
scanf(“%un”, &sr_ptr->age);
scanf(“%un”, &sr_ptr->enroll_no);
printf(“See entered valuesn”);
printf(“%dn”, sr_ptr->id_no);
printf(“%un”, sr_ptr->age);
printf(“%un”, sr_ptr->enroll_no);
return 0;
}
_________________________________
The same program can also be written as
___________________________________
#include<stdio.h>
struct student_record
{
int id_no;
int age;
int enroll_no;
};
int main()
{
struct student_record sr, *sr_ptr;
sr_ptr= &sr;
int size;
printf(“Enter the values of sr.id_no, sr.age and sr.enroll_non”);
scanf(“%dn”, *sr_ptr.id_no);
scanf(“%un”, *sr.age);
scanf(“%un”, *sr_ptr.enroll_no);
printf(“See entered valuesn”);
printf(“%dn”, *sr_ptr.id_no);
printf(“%un”, *sr_ptr.age);
printf(“%un”, *sr_ptr.enroll_no);
return 0;
}
Because *sr_ptr variable means value at the address the pointer sr_ptr pointing to.
Conclusion- In this post, I have discussed structures, hope it will be helpful for you.