Structure Pointer In C - C Programming Tutorial
C Course / Programs / Structure Pointer In C

Structure Pointer In C

BLUF: Understanding Structure Pointer In C is a foundational part of learning C programming. This tutorial explains the core principles and syntax needed to implement this concept effectively.
Core Programming Principle: Structure Pointer In C

C provides direct access to memory and system resources. Learn how Structure Pointer In C leverages this power in the lesson below.

Syntax to define Structure

Example

struct structure_name
{
char name[40];
int roll_no;
float percentage;
char sub[30];
};
struct structure_name  str;  // str is a variable of the structure_name

Here, structurename refers to the identifier assigned to a specific Structure created with the struct keyword. The structurename encapsulates various data types (such as int, char, and float) as its members. Lastly, str represents a variable associated with this Structure.

Program to demonstrate the structure and access their member

In this code snippet, a student structure is defined and its members are accessed using the structure variable along with the dot (.) operator.

struct.c

Example

#include <stdio.h>
#include <string.h>

// create the Structure of student to store multiples items
struct student
{
	char name[ 30];
	int roll_no;
	char state[100];
	int age;
};
struct student s1, s2; // declare s1 and s2 variables of student structure

int main()
{
	// records of the student s1
	strcpy (s1.name, "John");
	s1.roll_no = 1101;
	strcpy (s1.state, "Los Angeles");
	s1.age = 20;	
	
	// records of the student s2
	strcpy (s2.name, " Mark Douglas");
	s2.roll_no = 111;
	strcpy (s2.state, "California");
	s2.age = 18;
	
	// print the details of the student s1;
	printf (" Name of the student s1 is: %s\t ", s1.name);
    printf (" \n Roll No. of the student s1 is: %d\t ", s1.roll_no);
    printf (" \n The state of the student s1 is: %s\t ", s1.state);
    printf (" \n Age of the student s1 is: %d\t ", s1.age);
    
    
    	// print the details of the student s2;
	printf ("\n Name of the student s1 is: %s\t ", s2.name);
    printf (" \n Roll No. of the student s1 is: %d\t ", s2.roll_no);
    printf (" \n The state of the student s1 is: %s\t ", s2.state);
    printf (" \n Age of the student s1 is: %d\t ", s2.age);
    return 0;
}

Output:

Output

Name of the student s1 is: John
Roll No. of the student s1 is: 1101
 state of the student s1 is: Los Angeles
Age of the student s1 is: 20
Name of the student s1 is:  Mark Douglas
Roll No. of the student s1 is: 111
 The state of the student s1 is: California
Age of the student s1 is: 18

In the provided program, a structure named "student" is defined, which includes various elements like name (char), roll_no (int), state (char), and age (int). Additionally, the structure declares two variables, s1 and s2, which are utilized to access the members of the structure by employing the dot operator within the main function.

Structure Pointer

The structure pointer indicates the memory location where the structure is stored. It functions similarly to a regular pointer that references the address of a different variable stored in memory, regardless of its data type (such as int, char, or float). In this case, a structure pointer is employed to specify the memory address of a structure by assigning the pointer variable ptr to the structure variable.

Declare a Structure Pointer

The way we declare a pointer to a structure resembles how we declare a variable for the structure. As a result, it is possible to define both the structure pointer and variable within or outside the main function. In C, when declaring a pointer variable, the name of the variable is preceded by the asterisk (*) symbol.

Example

struct structure_name *ptr;

After establishing the structure pointer, it is essential to set it up initially, as demonstrated in the following code snippet:

Initialization of the Structure Pointer

Example

ptr = &structure_variable;

A Structure Pointer can be initialized directly when declaring a pointer.

Example

struct structure_name *ptr = &structure_variable;

A pointer labeled ptr is pointing to the memory address structure_variable of the Structure.

Access Structure member using pointer:

There are two methods to retrieve the member of the structure using a Structure pointer:

  • One way is through the ( * ) asterisk or the indirection operator combined with the dot ( . ) operator.
  • The other method involves using the arrow ( -> ) operator or the membership operator.
  • Program to access the structure member using structure pointer and the dot operator

Let's explore an instance of establishing a Subject framework and retrieving its elements by employing a structure pointer that points to the memory location of the Subject variable in the C programming language.

Pointer.c

Example

#include <stdio.h>

// create a structure Subject using the struct keyword
struct Subject
{
	// declare the member of the Course structure
	char sub_name[30];
	int sub_id;
	char sub_duration[50];
	char sub_type[50];
};

int main()
{
	struct Subject sub; // declare the Subject variable
	struct Subject *ptr; // create a pointer variable (*ptr) 
	ptr = ⊂ /* ptr variable pointing to the address of the structure variable sub */
	
	strcpy (sub.sub_name, " Computer Science");
	sub.sub_id = 1201;
	strcpy (sub.sub_duration, "6 Months");
	strcpy (sub.sub_type, " Multiple Choice Question");

	// print the details of the Subject;
	printf (" Subject Name: %s\t ", (*ptr).sub_name);
            printf (" \n Subject Id: %d\t ", (*ptr).sub_id);
    	printf (" \n Duration of the Subject: %s\t ", (*ptr).sub_duration);
           printf (" \n Type of the Subject: %s\t ", (*ptr).sub_type);

	return 0;
	
}

Output:

Output

Subject Name:  Computer Science
 Subject Id: 1201
 Duration of the Subject: 6 Months
 Type of the Subject:  Multiple Choice Question

In the preceding code snippet, a Subject structure has been defined, encompassing various data components such as subname (character), subid (integer), subduration (character), and subtype (character). Within this context, 'sub' serves as the structure variable, while 'ptr' functions as the structure pointer variable that points to the address of the 'sub' variable, indicated by ptr = &sub. Consequently, each *ptr instance is utilized to access the address of a specific member within the Subject structure.

Program to access the structure member using structure pointer and arrow (->) operator

Let's explore a program that accesses the structure elements using pointers and the arrow (->) operator in the C programming language.

Pointer2.c

Example

#include <stdio.h>

// create Employee structure 
struct Employee
{
	// define the member of the structure
	char name[30];
	int id;
	int age;
	char gender[30];
	char city[40];
};

// define the variables of the Structure with pointers
struct Employee emp1, emp2, *ptr1, *ptr2;

int main()
{
	// store the address of the emp1 and emp2 structure variable
	ptr1 = &emp1;
	ptr2 = &emp2;
	
	printf (" Enter the name of the Employee (emp1): ");
	scanf (" %s", &ptr1->name);
	
	printf (" Enter the id of the Employee (emp1): ");
	scanf (" %d", &ptr1->id);
	printf (" Enter the age of the Employee (emp1): ");
	scanf (" %d", &ptr1->age);
	printf (" Enter the gender of the Employee (emp1): ");
	scanf (" %s", &ptr1->gender);
	printf (" Enter the city of the Employee (emp1): ");
	scanf (" %s", &ptr1->city);
	
	printf (" \n Second Employee: \n");
            printf (" Enter the name of the Employee (emp2): ");
	scanf (" %s", &ptr2->name);
	
	printf (" Enter the id of the Employee (emp2): ");
	scanf (" %d", &ptr2->id);
	printf (" Enter the age of the Employee (emp2): ");
	scanf (" %d", &ptr2->age);
	printf (" Enter the gender of the Employee (emp2): ");
	scanf (" %s", &ptr2->gender);
	printf (" Enter the city of the Employee (emp2): ");
	scanf (" %s", &ptr2->city);
	
	printf ("\n Display the Details of the Employee using Structure Pointer");
	printf ("\n Details of the Employee (emp1) \n");
	printf(" Name: %s\n", ptr1->name);
	printf(" Id: %d\n", ptr1->id);
	printf(" Age: %d\n", ptr1->age);
	printf(" Gender: %s\n", ptr1->gender);
	printf(" City: %s\n", ptr1->city);
	
	printf ("\n Details of the Employee (emp2) \n");
	printf(" Name: %s\n", ptr2->name);
	printf(" Id: %d\n", ptr2->id);
	printf(" Age: %d\n", ptr2->age);
	printf(" Gender: %s\n", ptr2->gender);
	printf(" City: %s\n", ptr2->city);
	return 0;
}

Output:

Output

Enter the name of the Employee (emp1): John
 Enter the id of the Employee (emp1): 1099
 Enter the age of the Employee (emp1): 28
 Enter the gender of the Employee (emp1): Male
 Enter the city of the Employee (emp1): California

 Second Employee:  
 Enter the name of the Employee (emp2): Maria
 Enter the id of the Employee (emp2): 1109
 Enter the age of the Employee (emp2): 23
 Enter the gender of the Employee (emp2): Female
 Enter the city of the Employee (emp2): Los Angeles

 Display the Details of the Employee using Structure Pointer
 Details of the Employee (emp1) 
 Name: John
 Id: 1099
 Age: 28
 Gender: Male
 City: California

 Details of the Employee (emp2) Name: Maria
 Id: 1109
 Age: 23
 Gender: Female
 City: Los Angeles

In the preceding code snippet, an Employee structure has been defined, encompassing two structure instances named emp1 and emp2, along with pointer variables ptr1 and ptr2. This Employee structure includes attributes such as name, id, age, gender, and city. Each member of the Employee structure acquires its specific value sequentially from the user by leveraging the pointer variable and arrow operator to ascertain their memory allocation.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience