The Ultimate Guide to Understanding Structure Pointers

Structure Pointer

A structure pointer is a pointer that refers to the structure.

struct store *str;  /* structure pointer is str */

Once the structure pointer is declared, we can use ‘structure pointer member operator’ or ‘arrow operator’ to access the elements of the structure. Structure pointer member operator is represented by arrow symbol, as ->.

ptr->bookno;
ptr->bookname;
ptr->price;

Program to print electricity bill using a structure pointer

#include<stdio.h>
#include<string.h>
void main()
{
   struct ebill
   {
     int mno;
     char name[20];
     int previous;
     int units;
     float charge;
   }
   struct ebil *ptr;
   ptr->mno = 1005;
   strcpy(ptr->name, "Nav");
   ptr->previous = 500;
   ptr->present = 750;
   ptr->units = ptr->present - ptr->previous;
   ptr->charge = ptr->units * 4.50;
   printf("\nMeter no: %d",ptr->mno);
   printf("\nCustomer no: %d", ptr->name);
   printf("\nPrevious reading: %d",ptr->previous);
   printf("\nPresent reading: %d", ptr->present);
   printf("\nTotal units: %d", ptr->units);
   printf("\nTotal charge: %8.2f", ptr->charge);
}

Output

Meter no: 1005
Customer name: Nav
Previous reading: 500
Total units: 250
Total charge: 1125.00
Naveed Tawargeri
 

Hi, I'm Naveed Tawargeri, and I'm the owner and creator of this blog. I'm a Software Developer with a passion for Programming.