- C Programming Tutorial
- C - Home
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Booleans
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- C - Storage Classes
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- C - Functions
- C - Main Functions
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Passing Pointers to Functions
- C - Strings
- C - Array of Strings
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Pointers to Structures
- C - Self-Referential Structures
- C - Nested Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C - Pointers to Structures
In C, a pointer is a variable that holds the address of another variable. If you have defined a derived data type with the struct keyword, you can declare a variable of this type, and 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.
A new derived data type is defined with struct keyword as following syntax −
struct type{ type var1; type var2; type var3; . . . . . . };
You can then declare a variable of this derived data type as −
struct type var;
You can then declare a pointer variable and store the address of var. We know that to declare a variable as a pointer, it must be prefixed by *, and to obtain the address of a variable, we use the & operator.
struct type *ptr = &var;
To access the elements of the structure with pointer, we use a special operator called as an indirection operator ( −>> ).
A user−defined struct type book is defined below. We declare a book variable and a pointer.
struct book{ char title[10]; double price; int pages; }; struct book b1 = {"Learn C", 675.50, 325}, struct book *strptr;
To store the address, use the & operator.
strptr = &b1;
C defines −>: symbol to be used with struct pointer as indirection operator (also called struct dereference operator). It helps to access the elements of the struct variable to which the pointer reference to.
To access an individual element in the struct, the indirection operator is used as follows
Ptr-> membername;
For example −
strptr->title, strptr->price, strptr->pages
The struct pointer uses indirection operator or 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
b1.title is the same as strpr->title b1.price is the same as strptr->price b1.pages is the same as strptr->pages
The following program shows the usage of pointers to structures −
In this example, strptr is a pointer to struct book b1 variable. Hence, strrptr−>title returns the title, similar to b1.title does.
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
It may be noted that −
- 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
Consider another example which explains the functioning of pointers to structures. A new derived data type person is defined with the struct keyword. A variable of its type and a pointer is declared.
The user is asked to input name, age and weight. The values are stored in the structure elements by accessing them with the indirection operator −>
#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: \n"); printf("Name: %s\n", personPtr->name); printf("Age: %d\n", personPtr->age); printf("weight: %f", personPtr->weight); return 0; }
Output
Let us run the above program that will produce the following result −
Displaying: Name: Meena Age: 40 weight: 60.000000
C allows you to declare an array of struct, as well as an array of pointers. Here, each element in the struct pointer array is a reference to a struct variable.
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 the function.
You may have noted that you need to prefix struct type to the name of the variable or pointer at the time of declaration. This can be avoided by creating a shorthand notation with the help of typedef keyword, which we shall learn in a latter chapter.
Pointers to structures are very important, as they are employed to create complex and dynamic data structures such as linked lists, trees, graphs, etc. Such data structures use self−referential structs, where we define a struct type that 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 −
struct mystruct{ int a; struct mystruct *b; };
We shall learn about self−referential structures in the next chapter.
To Continue Learning Please Login
Login with Google