A structure pointer is a pointer variable that stores the address of a structure. It allows the programmer to manipulate the structure and its members directly by referencing their memory location rather than passing the structure itself. In this article let's take a look at structure pointer in C.
Let's take a look at an example:
C
#include <stdio.h>
struct A {
int var;
};
int main() {
struct A a = {30};
// Creating a pointer to the structure
struct A *ptr;
// Assigning the address of person1 to the pointer
ptr = &a;
// Accessing structure members using the pointer
printf("%d", ptr->var);
return 0;
}
Explanation: In this example, ptr is a pointer to the structure A. It stores the address of the structure a, and the structure's member var is accessed using the pointer with the -> operator. This allows efficient access to the structure's members without directly using the structure variable.
Syntax of Structure Pointer
The syntax of structure pointer is similar to any other pointer to variable:
struct struct_name *ptr_name;
Here, struct_name is the name of the structure, and ptr_name is the name of the pointer variable.
Accessing Member using Structure Pointers
There are two ways to access the members of the structure with the help of a structure pointer:
- Differencing and Using (.) Dot Operator.
- Using ( -> ) Arrow operator.
Differencing and Using (.) Dot Operator
First method is to first dereference the structure pointer to get to the structure and then use the dot operator to access the member. Below is the program to access the structure members using the structure pointer with the help of the dot operator.
C
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[30];
char branch[40];
int batch;
};
int main() {
struct Student s1 = {27, "Geek", "CSE", 2019};
// Pointer to s1
struct Student* ptr = &s1;
// Accessing using dot operator
printf("%d\n", (*ptr).roll_no);
printf("%s\n", (*ptr).name);
printf("%s\n", (*ptr).branch);
printf("%d", (*ptr).batch);
return 0;
}
Using ( -> ) Arrow Operator
C language provides an array operator (->) that can be used to directly access the structure member without using two separate operators. Below is the program to access the structure members using the structure pointer with the help of the Arrow operator.
C
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[30];
char branch[40];
int batch;
};
int main() {
struct Student s1 = {27, "Geek", "CSE", 2019};
// Pointer to s1
struct Student* ptr = &s1;
// Accessing using dot operator
printf("%d\n", ptr->roll_no);
printf("%s\n", ptr->name);
printf("%s\n", ptr->branch);
printf("%d", ptr->batch);
return 0;
}
Explanation: In this code, a struct Person is defined with name and age as members. A pointer ptr is used to store the address of person1. The arrow operator (->) is used to access and modify the members of the structure via the pointer, updating the name and age of person1, and printing the updated values.
Similar Reads
Structure of the C Program The basic structure of a C program is divided into 6 parts which makes it easy to read, modify, document, and understand in a particular format. C program must follow the below-mentioned outline in order to successfully compile and execute. Debugging is easier in a well-structured C program.Sections
5 min read
C Structures In C, a structure is a user-defined data type that can be used to group items of possibly different types into a single type. The struct keyword is used to define a structure. The items in the structure are called its member and they can be of any valid data type.Example:C#include <stdio.h> //
9 min read
Nested Structure in C with Examples A nested structure in C is a structure within a structure. One structure can be declared inside another structure in the same way structure members are declared inside a structure.Example: C#include <stdio.h> #include <string.h> // Declaration of Outer structure struct College { char col
9 min read
Read/Write Structure From/to a File in C For writing in the file, it is easy to write string or int to file using fprintf and putc, but you might have faced difficulty when writing contents of the struct. fwrite and fread make tasks easier when you want to write and read blocks of data.Writing Structure to a File using fwriteWe can use fwr
3 min read
Queue using Linked List in C Queue is a linear data structure that follows the First-In-First-Out (FIFO) order of operations. This means the first element added to the queue will be the first one to be removed. There are different ways using which we can implement a queue data structure in C.In this article, we will learn how t
7 min read
Segment Tree in C A Segment Tree is a data structure in C that helps us quickly perform operations (like finding the sum, minimum, or maximum) on a range of elements in an array. Itâs like a tree that stores information about parts of the array in each node. In this article, we will learn what are segement trees, how
5 min read
Difference between Structure and Array in C Array in C An array is collection of items stored at contiguous memory locations. Structure in C A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. Difference between Structure and Array AR
2 min read
Difference Between Structure and Union in C In C programming, both structures and unions are used to group different types of data under a single name, but they behave in different ways. The main difference lies in how they store data.The below table lists the primary differences between the C structures and unions:ParameterStructureUnionDefi
4 min read
Stack Using Linked List in C Stack is a linear data structure that follows the Last-In-First-Out (LIFO) order of operations. This means the last element added to the stack will be the first one to be removed. There are different ways using which we can implement stack data structure in C. In this article, we will learn how to i
7 min read
Postorder Tree Traversal in Binary Tree in C A binary tree is a hierarchical data structure in computer science. Each node in a binary tree can have at most two children: a left child and a right child. There are several ways to traverse a binary tree and in this article, we will learn about the postorder traversal of a binary tree in C. Examp
3 min read