¤ 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;

Example:
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>
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++;
	}
}




Share:




comments powered by Disqus


Web Service Offers

Third Level Dot In Registration
599 199 (66.78% off)
per Year

.IN Registration
749 199 (73.43% off)
per Year

Ecommerce on the Cloud

An online store hosted on the Cloud. Sell unlimited products. Integrated business functions. Well-suited for high traffic website.

39,999 29,999 (25% off)

Ecommerce Premium

An e-commerce website well-suited for companies having a large portfolio of products. Sell unlimited products. Manage both offline and online sales from one system.

40,000 25,000 (37.5% off)

Ecommerce Business

Launch your online business. Sell up to 5000 products. Collect payment via multiple online and offline payment methods.

35,000 25,000 (28.57% off)

Ecommerce Pro

Get a Mobile friendly online store front. Sell up to 1000 products.

35,000 25,000 (28.57% off)

Ecommerce Starter

Start selling your products online. Scale up as your business grows.

35,000 25,000 (28.57% off)

Website on the Cloud

Custom built, responsive corporate website hosted on the cloud. Mirrored on 3 servers. Includes visitor signup with CRM functions/

19,999 9,999 (50% off)

Website Corporate

Custom built, responsive corporate website with CRM. Our most popular website plan.

19,000 9,000 (52.63% off)

Website Business - 1000 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

18,000 8,000 (55.56% off)

Website Business - 500 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,500 7,500 (57.14% off)

Website Business - 100 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,000 7,000 (58.82% off)

Website Business - 50 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,500 6,500 (60.61% off)

Website Business - 25 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,000 6,000 (62.5% off)

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% off)


Looking to build a website?
Launch a 5 page website in 1 day at only Rs.499/year. Hosting 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