...

/

Structure and Pointers

Structure and Pointers

Learn how to create a pointer to a structure and an array of structures in C.

What is a structure pointer?


A pointer pointing to a struct is known as a structure pointer.


Example program

The program given below demonstrates the use of a structure pointer.

Press + to interact
# include <stdio.h>
int main( )
{
// Declare structure book
struct book
{
char n[ 20 ] ;
int nop ;
float pr ;
} ;
// Initialize structure variable
struct book b = { "Basic", 425, 135.00 } ;
// Declare pointer to structure variable
struct book *ptr;
// Store address of structure variable
ptr = &b;
// Access structure elements using dot operator
printf ( "%s %d %f\n", b.n, b.nop, b.pr ) ;
// Access structure elements using arrow operator
printf ( "%s %d %f\n", ptr->n, ptr->nop, ptr->pr ) ;
}

Explanation

In the code given above, ptr is a pointer to a structure, ...

Access this course and 1400+ top-rated courses and projects.