

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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\n"); printf ("Number = %d\n", st ->sno); printf ("name = %s\n", 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 Questions & Answers
- 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
- Differentiate the NULL pointer with Void pointer in C language
- How to access array elements using a pointer in C#?
- How to access an array element in C language?
- How to create a pointer for strings using C language?
- Structure declaration in C language
- How to access elements of an array using pointer notation in C#?
- Double Pointer (Pointer to Pointer) in C
- Explain the concept of pointer accessing in C language
- What is void pointer in C language?
- C++ Program to Access Elements of an Array Using Pointer
- How to pass the address of structure as an argument to function in C language?