Pointers to Structures in C
Explore how to use pointers with structures in C programming. Learn how to define a struct, create variables of this type, and declare pointer variables that store the addresses of these structure variables. Understand the benefits of using pointers with structures for efficient data management and manipulation.
Pointers to Structures in C
When you define a derived data type using the struct
keyword, you can create a variable of this type. Additionally, you can declare a pointer variable to store the address of the structure variable. A pointer to a struct is thus a variable that refers to a struct variable.
Syntax: Defining and Declaring a Structure
To define a new derived data type using the struct
keyword, use the following syntax:
Code Example
struct type {
type var1;
type var2;
type var3;
...
};
You can then declare a variable of this derived data type as follows:
Code Example
struct type var;
To declare a pointer variable and store the address of the struct variable, use:
Code Example
struct type *ptr = &var;
Accessing the Elements of a Structure
To access the elements of a structure using a pointer, we use a special operator called the indirection operator (→
), also known as the struct dereference operator.
Here’s how to define a user-defined struct type called book
and declare a variable and a pointer:
Code Example
struct book {
char title[10];
double price;
int pages;
};
struct book b1 = {"Learn C", 675.50, 325};
struct book *strptr;
To store the address of the structure variable, use the &
operator:
Code Example
strptr = &b1;
Using the Indirection Operator
To access individual elements in a struct using a pointer, apply the indirection operator:
Code Example
strptr -> title;
strptr -> price;
strptr -> pages;
In this case:
strptr -> title
is equivalent tob1.title
strptr -> price
is equivalent tob1.price
strptr -> pages
is equivalent tob1.pages
Example: Pointers to Structures
The following program demonstrates the usage of pointers to structures. In this example, strptr
is a pointer to the variable b1
.
Code Example
#include <stdio.h>
#include <string.h>
struct book {
char title[10];
double price;
int pages;
};
int main() {
struct book b1 = {"Learn C", 675.50, 325};
struct book *strptr;
strptr = &b1;
printf("Title: %s\n", strptr -> title);
printf("Price: %lf\n", strptr -> price);
printf("No of Pages: %d\n", strptr -> pages);
return 0;
}
Output
Title: Learn C
Price: 675.500000
No of Pages: 325
Points to Note
- The dot operator (
.
) is used to access the struct elements via the struct variable. - To access the elements via its pointer, the indirection operator (
→
) must be used.
Another Example
Let's consider another example to understand how pointers to structures work. Here, we will define a new derived data type called person
:
Code Example
#include <stdio.h>
#include <string.h>
struct person {
char *name;
int age;
float weight;
};
int main() {
struct person *personPtr, person1;
strcpy(person1.name, "Meena");
person1.age = 40;
person1.weight = 60;
personPtr = &person1;
printf("Displaying the Data: \n");
printf("Name: %s\n", personPtr -> name);
printf("Age: %d\n", personPtr -> age);
printf("Weight: %f", personPtr -> weight);
return 0;
}
Output
Displaying the Data:
Name: Meena
Age: 40
Weight: 60.000000
Arrays of Structures and Pointers
C allows you to declare both an "array of structs" and an "array of pointers." Each element in the struct pointer array references a struct variable.
A struct variable behaves like a normal variable of a primary type, which means you can create an array of structs, pass the struct variable to a function, and return a struct from a function.
Note: You need to prefix struct type
to the name of the variable or pointer at the time of declaration. However, you can avoid this by using the typedef
keyword.
Why Do We Need Pointers to Structures?
Pointers to structures are crucial for creating complex and dynamic data structures, such as linked lists, trees, and graphs. Such structures often use self-referential structs, where a struct type has one of its elements as a pointer to the same type.
An example of a self-referential structure with a pointer to an element of its own type is defined as follows:
Code Example
struct mystruct {
int a;
struct mystruct *b;
};