
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - 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 - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What are pointers to structures in C language?
Pointer to structure holds the add of the entire structure.
It is used to create complex data structures such as linked lists, trees, graphs and so on.
The members of the structure can be accessed using a special operator called as an arrow operator ( -> ).
Declaration
Following is the declaration for pointers to structures in C programming −
struct tagname *ptr;
For example − struct student *s −
Accessing
It is explained below how to access the pointers to structures.
Ptr-> membername;
For example − s->sno, s->sname, s->marks;
Example Program
The following program shows the usage of pointers to structures −
#include<stdio.h> struct student{ int sno; char sname[30]; float marks; }; main ( ){ struct student s; struct student *st; printf("enter sno, sname, marks:"); scanf ("%d%s%f", & s.sno, s.sname, &s. marks); st = &s; printf ("details of the student are"); printf ("Number = %d
", st ->sno); printf ("name = %s
", st->sname); printf ("marks =%f
", st ->marks); getch ( ); }
Output
Let us run the above program that will produce the following result −
enter sno, sname, marks:1 Lucky 98 details of the student are: Number = 1 name = Lucky marks =98.000000
Example 2
Consider another example which explains the functioning of pointers to structures.
#include<stdio.h> struct person{ int age; float weight; }; int main(){ struct person *personPtr, person1; personPtr = &person1; printf("Enter age: "); scanf("%d", &personPtr->age); printf("Enter weight: "); scanf("%f", &personPtr->weight); printf("Displaying:
"); printf("Age: %d
", personPtr->age); printf("weight: %f", personPtr->weight); return 0; }
Output
Let us run the above program that will produce the following result −
Enter age: 45 Enter weight: 60 Displaying: Age: 45 weight: 60.000000
- Related Articles
- What are nested structures in C language?
- What are the different types of pointers in C language?
- What are different pointer operations and problems with pointers in C language?
- Explain the pointers to unions in C language
- What are pointers in C#?
- What is an array of structures in C language?
- What are Wild Pointers in C/C++?
- Explain Arithmetic operations using pointers in C language?
- Explain Near Far Huge pointers in C language
- Explain the concept of pointers in C language
- Explain array of pointers in C programming language
- Explain pointers and two-dimensional array in C language
- Explain pointers and one-dimensional array in C language
- Demonstrate the concept of pointers using C language
- Explain structures using typedef keyword in C language
