
- 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
How to access the pointer to structure in C language?
Pointer to structure holds the address of an entire structure.
Mainly, these are used to create the complex data structures such as linked lists, trees, graphs and so on.
The members of the structure can be accessed by using a special operator called arrow operator ( -> ).
Declaration
Following is the declaration for pointer to structure −
struct tagname *ptr;
For example, struct student *s;
Accessing
You can access pointer to structure by using the following −
Ptr-> membername;
For example, s->sno, s->sname, s->marks;
Example
Following is the C program to access the pointer to structure −
#include<stdio.h> struct classroom{ int students[7]; }; int main(){ struct classroom clr = {2, 3, 5, 7, 11, 13}; int *ptr; ptr = (int *)&clr; printf("%d",*(ptr + 4)); return 0; }
Output
When the above program is executed, it produces the following result −
11
Explanation
Here, a pointer variable ptr holds the address of a first value 2 of an object clr. Then, the address of the pointer variable is incremented by 4 and finally, the value is displayed.
For example,
*(ptr + 0) = 2 *(ptr + 1) = 3 *(ptr + 2) = 5 *(ptr + 3) = 7 *(ptr + 4) = 11 *(ptr + 5) = 13
Consider another simple example to know about pointer to structures −
Example
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
When the above program is executed, it produces the following result −
enter sno, sname, marks :1 bhanu 69 details of the student are Number = 1 name = bhanu marks =69.000000
- Related Articles
- How to define pointer to pointer in C language?
- Explain the dynamic memory allocation of pointer to structure in C language
- Explain the concept of pointer to pointer and void pointer in C language?
- Explain the Union to pointer in C language
- How to access array elements using a pointer in C#?
- How to access an array element in C language?
- Differentiate the NULL pointer with Void pointer in C language
- How to create a pointer for strings using C language?
- How to access elements of an array using pointer notation in C#?
- C++ Program to Access Elements of an Array Using Pointer
- What is void pointer in C language?
- Explain the concept of pointer accessing in C language
- Structure declaration in C language
- Double Pointer (Pointer to Pointer) in C
- How to pass entire structure as an argument to function in C language?
