You can declare pointers that can point to a structure. The syntax for such a declaration is given below:
typedef struct { char first_name[21]; char middle_name[21]; char last_name[21]; } name; typedef struct { name *sname; int roll_no; float score; char grade[3]; } student; student *ps, a;
In the above example, a is a variable for which memory is allocated to accommodate student information, while ps is a pointer to a structure but not having any valid address to point to as yet. Note that we have further enhanced the declaration by using typedef to define our own struct data type.
The proper initialization of the pointer variable ps with valid memory space can be done by assigning the address of a structure variable e.g.,
Alternatively ps can be assigned a valid address using the malloc function to dynamically allocate space.
The type casting of the output of function malloc to student type by using (student*)
, ensures proper initialization of the pointer ps.
A member of the student structure can now be referenced by using the pointer variable, followed by the symbol (->) and the member name, e.g.,
Since sname is also a pointer (to type struct name), memory space needs to be allocated for this variable as well, as follows.
In the above statement, malloc allocates memory space having size equal to the size of the structure name.
Now let us look at a practical implementation.
/* Program to read student data and print the same */ #include <stdio.h> main() { int i, j; float x, y; char a; typedef struct { char name[26]; char roll_no[8]; float sgpa[8]; float cgpa[8]; } stud_rec; stud_rec *stud; stud = (stud_rec *) malloc(sizeof(stud_rec)); printf("\nName of the Student?: "); for(i = 0; (a = getchar()) != '\n'; stud->name[i++] = a); stud->name[i] = '\0'; printf("\nRoll Number of %s?:", stud->name); for(i = 0; (a = getchar()) != '\n'; stud->roll_no[i++] = a) stud->name[i] = '\0'; printf("\nNumber of Semesters?:"); scanf("%d", &j); fflush(stdin); for(i=0; i < j; i++) { printf("\nSGPA in Semester %d?:", i+1); scanf("%f", &(stud->sgpa[i])); fflush(stdin); } for(x = 0.0, i = 0; i < j;) { x += (stud->sgpa[i]); stud->cgpa[i] = x / (i + 1); printf("CGPA in Semester %d is %5.2f\n", i+1, stud->cgpa[i]); i++; } }
Introduction to Structures in C
How to pass Structure as a parameter to a function in C?
Self Referential Data Structure in C - create a singly linked list
How to move your Email accounts from one hosting provider to another without losing any mails?
How to resolve the issue of receiving same email message multiple times when using Outlook?
Self Referential Data Structure in C - create a singly linked list
Mosquito Demystified - interesting facts about mosquitoes
Elements of the C Language - Identifiers, Keywords, Data types and Data objects
How to pass Structure as a parameter to a function in C?
Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.
Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.