¤ Home » Programming » C Tutorial » Defining Pointers to Structures in C

Defining Pointers to Structures in C

You can declare pointers that can point to a structure. The syntax for such a declaration is given below:

<type> *ptvar;

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.,

ps = &a;

Alternatively ps can be assigned a valid address using the malloc function to dynamically allocate space.

ps = (student *) malloc (sizeof(student));

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.,

ps->score - to access score

Since sname is also a pointer (to type struct name), memory space needs to be allocated for this variable as well, as follows.

ps->sname = (name *) malloc(sizeof(name));

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>
	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]));

	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]); 


comments powered by Disqus

Buy Domain & Hosting from the most reliable and trusted company - WebServicesWorldWide.com.

Looking to build a website?
Launch a 20 page website in 1 day at only Rs.200/month or US$ 3.19/month. Hosting & Email included.

About the Author

Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

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 XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.

Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy