Pointer to Structure in C Programming Language

Rumman Ansari   Software Engineer   2023-03-27   9469 Share
☰ Table of Contents

Table of Content:


You have seen how to define a pointer to point to a basic data type such as an int or a char. But you can also define a pointer to point to a structure. Pointer which stores address of structure is called as Pointer to Structure.

We can define our date structure as follows:


struct date
{
int month;
int day;
int year;
} ;

variables to be of type struct date

struct date todaysDate;

We can define a variable to be a pointer to a struct date variable:

struct date *datePtr;

You can then use the variable datePtr, as just defined,in the expected fashion.For example, you can set it to point to todaysDate with the following assignment statement:

datePtr = &todaysDate;

After such an assignment, you can indirectly access any of the members of the date structure that datePtr points to in the following way:

(*datePtr).day = 21;

This statement sets the day of the date structure pointed to by datePtr to 21 .The parentheses are required because the structure member operator period ( .) has higher precedence than the indirection operator asterisk ( *) .

Pointers to structures are so often used that the language has a special operator.The structure pointer operator ->, which is a hyphen followed by the greater-than sign, permits expressions that would otherwise be written as

(*datePtr).day

to be more clearly expressed as

datePtr–>day
  1. Address of Pointer variable can be obtained using ‘&’ operator.
  2. Address of such Structure can be assigned to the Pointer variable.
  3. Pointer Variable which stores the address of Structure must be declared as Pointer to Structure.
  4. (*) and -> both represents the same.

Example Program

// Program to illustrate structure pointers
#include7lt;stdio.h>
int main()
{
 
struct date
{
	int month;
	int day;
	int year;
} ;

struct date today, *datePtr;

datePtr = &today;

	datePtr->month = 12;
	datePtr->day = 02;
	datePtr->year = 2017;
printf("Today's date is %i/%i/%.2i.",
datePtr->month, datePtr->day, datePtr->year % 100);
 
return 0;
}

Output

Today's date is 12/2/17. 

Another Simple Example Pointer to Structure

Syntax :

struct studentStruct
{
    char name[10];
    int roll;
    int marks;
}student1;
struct studentStruct *ptr;
ptr = &student1;

How to Access Structure Members using pointer

Accessing Roll Number : (*ptr).roll
Accessing Name        : (*ptr).name

Pointer variable is declared and pointer to structure. Whenever we declare pointer variable ,address of the structure variable is assigned to the pointer variable.

Example Program

#include 
int main()
{

struct studentStruct {
    char name[10];
    int roll;
    int marks;
}student1 = {"Rambo",21,90};

struct studentStruct *ptr;
ptr = &student1;

printf("Roll Number : %d \n",(*ptr).roll);
printf("Marks of Student : %d \n",(*ptr).marks);

return 0;
}

Output

Roll Number : 21
Marks of Student : 90
Press any key to continue . . .

Access Structure Members Using Membership Operator

Accessing Roll Number : ptr->roll
Accessing Name        : ptr->name

Example Program

#include 
int main()
{

struct studentStruct {
    char name[10];
    int roll;
    int marks;
}student1 = {"Rambo",21,90};

struct studentStruct *ptr;
ptr = &student1;

printf("Roll Number : %d \n", ptr->roll);
printf("Marks of Student : %d \n", ptr->marks);

return 0;
}

Output

Roll Number : 21
Marks of Student : 90
Press any key to continue . . .

Whenever we access the member of the structure using the pointer we use arrow operator to access the member of structure.

Syntax Analysis

Example Program

#include  
struct studentStruct {
    char name[10];
    int roll;
    int marks;
};

int main()
{ 
struct studentStruct *ptr;
struct studentStruct student1 = {"Rambo",21,98};

ptr = &student1;
 
printf("Name: %s\t Roll: %d\t marks: %d\n",(*ptr).name,(*ptr).roll,(*ptr).marks);
printf("Name: %s\t Roll: %d\t marks: %d\n",ptr->name,ptr->roll,ptr->marks);
  
 return 0;
}

Output

Name: Rambo      Roll: 21        marks: 98
Name: Rambo      Roll: 21        marks: 98
Press any key to continue . . .

Copy a structure in C

There are many methods to copy one structure to another structure in C.

 

We can copy using direct assignment of one structure to another structure or

record2=record1;

we can use C inbuilt function memcpy() or

 ptr = &record1; 
 memcpy(record3, ptr, sizeof(record1));
 

we can copy by individual structure members.

 strcpy(record4.name, record1.name);
 record4.roll= record1.roll;
 record4.marks = record1.marks;
 

Example Program

#include 


struct studentStruct {
    char name[10];
    int roll;
    int marks;
}record1 = {"Rambo",21,90};
 
int main()
{

struct studentStruct record2, *record3, *ptr, record4; 
ptr = &record1;


printf("Name             : %s \n",(*ptr).name);
printf("Roll Number      : %d \n",(*ptr).roll);
printf("Marks of Student : %d \n",(*ptr).marks);


 // 1st method to copy whole structure to another structure
 record2=record1; 
 printf("\nRecord2 - Direct copy from record1 \n");
 printf("  Name : %s \n  Roll : %d\n  Marks : %d\n", 
            record2.name, record2.roll, record2.marks);
 
 // 2nd method to copy using memcpy function
 ptr = &record1; 
 memcpy(record3, ptr, sizeof(record1));
 printf("\nRecord ptr - copied from record1 using memcpy \n");
 printf("  Name : %s \n  Roll : %d\n  Marks : %d\n", 
            record2.name, record2.roll, record2.marks);



 // 3rd method to copy by individual members
 printf("\nRecord4 - Copied individual members from record1 \n");
 strcpy(record4.name, record1.name);
 record4.roll= record1.roll;
 record4.marks = record1.marks;
    
 printf("  Name : %s \n  Roll : %d\n  Marks : %d\n", 
            record2.name, record2.roll, record2.marks);

return 0;
}
   

Output

Name             : Rambo
Roll Number      : 21
Marks of Student : 90

Record2 - Direct copy from record1
  Name : Rambo
  Roll : 21
  Marks : 90

Record ptr - copied from record1 using memcpy
  Name : Rambo
  Roll : 21
  Marks : 90

Record4 - Copied individual members from record1
  Name : Rambo
  Roll : 21
  Marks : 90
Press any key to continue . . .

Accessing structure member through pointer using dynamic memory allocation

To access structure member using pointers, memory can be allocated dynamically using malloc() function defined under "stdlib.h" header file.

Syntax to use malloc()

ptr = (cast-type*) malloc(byte-size)

Example to use structure's member through pointer using malloc() function.

Example Program

#include 


struct studentStruct {
    char name[10];
    int roll;
    int marks;
}record1 = {"Rambo",21,90};
 
int main()
{
  
   struct studentStruct *ptr;
   int i, num;

   printf("Enter number of Students: ");
   scanf("%d", &num);

   ptr = (struct studentStruct*) malloc(num * sizeof(struct studentStruct));
   /* Above statement allocates the memory for n structures with
    pointer personPtr pointing to base address */

   for(i = 0; i < num; ++i)
   {
       printf("Enter name, roll and marks of the Student respectively:\n");
       scanf("%s%d%d", &(ptr+i)->name, &(ptr+i)->roll, &(ptr+i)->marks);
   }

   printf("Displaying Infromation:\n");
   for(i = 0; i < num; ++i)
       printf("Name: %s\t Roll: %d\t Marks: %d\n",
	    (ptr+i)->name, (ptr+i)->roll, (ptr+i)->marks);

   return 0;
}
     

Output

Enter number of Students: 2
Enter name, roll and marks of the Student respectively:
Rambo
21
98
Enter name, roll and marks of the Student respectively:
Azmi
22
96
Displaying Infromation:
Name: Rambo      Roll: 21        Marks: 98
Name: Azmi       Roll: 22        Marks: 96
Press any key to continue . . .