In C programming, a structure pointer is a pointer that points to a structure variable. Using structure pointers makes it easier to access and manipulate the members of a structure, especially when passing structures to functions.
Structure Pointer in C
Declaration of Structure Pointer
struct struct_name {
data_type member1;
data_type member2;
…
};
struct struct_name *ptr;
- struct_name – Name of the structure.
- ptr – Pointer variable to store the address of the structure.
Accessing Structure Members via Pointer
To access structure members using a pointer, we use the arrow operator -> instead of the dot operator ..
ptr->member_name
This is equivalent to:
(*ptr).member_name
Example: Using Structure Pointer
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[50];
float marks;
};
int main() {
struct Student s1;
struct Student *ptr;
// Assigning values
ptr = &s1; // Pointer points to structure s1
ptr->roll_no = 101;
strcpy(ptr->name, “Rahul”);
ptr->marks = 95.5;
// Accessing structure members
printf(“Roll No: %d\n”, ptr->roll_no);
printf(“Name: %s\n”, ptr->name);
printf(“Marks: %.2f\n”, ptr->marks);
return 0;
}
Output:
Roll No: 101
Name: Rahul
Marks: 95.50
Passing Structure Pointer to Functions
Passing a structure pointer to a function saves memory and allows the function to modify the original structure.
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[50];
float marks;
};
void display(struct Student *s) {
printf(“Roll No: %d\n”, s->roll_no);
printf(“Name: %s\n”, s->name);
printf(“Marks: %.2f\n”, s->marks);
}
int main() {
struct Student s1 = {101, “Rahul”, 95.5};
display(&s1); // Pass structure pointer
return 0;
}
Output:
Roll No: 101
Name: Rahul
Marks: 95.50
Advantages of Structure Pointers
- Efficient memory usage – avoids copying the whole structure when passing to functions.
- Easy access to structure members dynamically.
- Allows modification of original structure values within functions.
- Useful when working with arrays of structures and dynamic memory allocation.
Summary
- Structure pointer points to the memory location of a structure variable.
- Access structure members using -> or (*ptr). syntax.
- Passing structure pointers to functions is memory efficient.
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System