Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Structures
  5. Pointers and Structures

Pointers and Structures

It is possible to create pointer to structure type. Pointers to structures have the following advantages,

  • It is easier to manipulate the pointers to structures than manipulating structures themselves
  • Passing a pointer to a structure as an argument to a function is efficient as compared to passing a structure to a function.
  • The size of the pointer to the structure is generally smaller than the size of a structure itself. 
  • Passing a pointer to a structure as an argument to a function requires less data movement as compared to passing structure to a function
  • Data structures like linked list, trees use structures containing pointer to structures

Declaring Pointer to a structure

The general form,

struct named_structure_type* identifier_name;
struct named_structure_type* identifier_name [=l-value];

A pointer to a structure type can be declared in a separate declaration statement only if the structure type is named. If the structure type is unnamed, the structure pointer should be created at the time of structure definition.

The declared structure variable can optionally be initialized with an l-value

Example : Declaration of structure variables

#include<stdio.h>
struct coord
{
	int x,y,z;
}pt1={2,3,5},*ptr1;
int main()
{
	struct coord pt2 = {4,5,6};
	struct coord *ptr2 = &pt2;
	ptr1 = &pt1;
	printf("Address of pt1: %p\n", &pt1);
	printf("Address of pt2: %p\n", &pt2);
	printf("Address of ptr1: %p\n", &ptr1);
	printf("Address of ptr2: %p\n", &ptr2);
	printf("ptr1 and ptr2 point to %p\t%p\n",ptr1,ptr2);
	printf("size of type (struct coord) is %d\n", sizeof(struct coord));
	printf("size of type (struct coord*) is %d\n", sizeof(struct coord*));	
	printf("size of pt1 and pt2 is %d\n", sizeof(pt1));
	printf("size of ptr1 and ptr2 is %d\n", sizeof(ptr2));	
	return 0;
}

Output

C:\TDM-GCC-64>gcc 4_pointer_Structure_1.c -o 4ps
C:\TDM-GCC-64>4ps
Address of pt1: 0000000000403010
Address of pt2: 000000000062FE10
Address of ptr1: 0000000000407A10
Address of ptr2: 000000000062FE08
ptr1 and ptr2 point to 0000000000403010 000000000062FE10
size of type (struct coord) is 12
size of type (struct coord*) is 8
size of pt1 and pt2 is 12
size of ptr1 and ptr2 is 8

Accessing structure Members via a pointer to a structure

The members of the structure object can be accessed via a pointer to a structure object by using one of the following ways,

  • By using dereference or indirection operator and the direct member access operator (.)
  • By using the indirect member access operator (-> arrow operator)

The general form to access structure member using dereference operator,

(*pointer_to_structure_type).structure_member_name

It is mandatory to parenthesize the dereference operator and the structure pointer because the dot operator has a higher precedence than dereference operator. The members of a structure object can also be accessed via the pointer to the structure object by using only one operator known as indirect member access operator.

pointer_to_structure_object->structure_member_name

Example

#include<stdio.h>
struct coord
{
	int x,y;
};
int main()
{
	struct coord pt ={2,3};
	struct coord *ptr = &pt;
	printf("coordinates of Pt1 are (%d,%d)\n",(*ptr).x,(*ptr).y);
	printf("coordinates of Pt1 are (%d,%d)\n",ptr->x,ptr->y);
	return 0;
}

Output

C:\TDM-GCC-64>gcc 4_pointer_Structure_2.c -o 4ps2
C:\TDM-GCC-64>4ps2
coordinates of Pt1 are (2,3)
coordinates of Pt1 are (2,3)

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments