Professional Documents
Culture Documents
Pointers To Structures in C
Pointers To Structures in C
Pointers To Structures in C
Pointers to Structures in C
If you have defined a derived data type using the keyword struct, then you can
declare a variable of this type. Hence, you can also declare a pointer variable to
store its address. A pointer to struct is thus a variable that refers to a struct variable.
struct type {
type var1;
type var2;
type var3;
...
...
};
You can then declare a variable of this derived data type as following −
You can then declare a pointer variable and store the address of var. To declare a
variable as a pointer, it must be prefixed by "*"; and to obtain the address of a
variable, we use the "&" operator.
Here, we define a user-defined struct type called book. We declare a book variable
and a pointer.
struct book{
char title[10];
double price;
https://www.tutorialspoint.com/cprogramming/c_pointers_to_structures.htm 1/5
6/16/24, 1:05 PM Pointers to Structures in C
int pages;
};
struct book b1 = {"Learn C", 675.50, 325},
struct book *strptr;
strptr = &b1;
The struct pointer uses the indirection operator or the dereference operator to fetch
the values of the struct elements of a struct variable. The dot operator (".") is used
to fetch the values with reference to the struct variable. Hence,
The following program shows the usage of pointers to structures. In this example,
"strptr" is a pointer to the variable "struct book b1". Hence, "strrptr → title" returns
the title, similar to "b1.title" does.
#include <stdio.h>
#include <string.h>
struct book{
char title[10];
double price;
https://www.tutorialspoint.com/cprogramming/c_pointers_to_structures.htm 2/5
6/16/24, 1:05 PM Pointers to Structures in C
int pages;
};
int main(){
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, we must use the indirection operator
(→).
Example
The user is asked to input the name, age and weight of the person. The values are
stored in the structure elements by accessing them with the indirection operator.
https://www.tutorialspoint.com/cprogramming/c_pointers_to_structures.htm 3/5
6/16/24, 1:05 PM Pointers to Structures in C
#include <stdio.h>
#include <string.h>
struct person{
char *name;
int age;
float weight;
};
int main(){
strcpy(person1.name, "Meena");
person1.age = 40;
person1.weight = 60;
personPtr = &person1;
return 0;
}
Output
When you runt this program, it will produce the following output −
A struct variable is like a normal variable of primary type, in the sense that you can
have an array of struct, you can pass the struct variable to a function, as well as
return a struct from a function.
https://www.tutorialspoint.com/cprogramming/c_pointers_to_structures.htm 4/5
6/16/24, 1:05 PM Pointers to Structures in C
Note: You need to prefix "struct type" to the name of the variable or pointer at the
time of declaration. However, you can avoid it by creating a shorthand notation using
the typedef keyword.
struct mystruct{
int a;
struct mystruct *b;
};
https://www.tutorialspoint.com/cprogramming/c_pointers_to_structures.htm 5/5