C program to demonstrate example of structure pointer (structure with pointer)

In this program, we will learn how to use structure with the pointers? How to declare a structure pointer (pointer to structure object), how to assign values to the structure members and how to access them using structure pointer?

In this example, we are implementing a structure item, declaring structure pointer pItem and by using pItem, we will assign and access the elements of the structure.

/*C program to demonstrate example of structure pointer (structure with pointer)*/
#include <stdio.h>

struct item
	char itemName[30];
	int qty;
	float price;
	float amount;

int main()
	struct item itm;	/*declare variable of structure item*/
	struct item *pItem;	/*declare pointer of structure item*/
	pItem = &itm;		/*pointer assignment - assigning address of itm to pItem*/
	/*read values using pointer*/
	printf("Enter product name: ");
	printf("Enter price:");
	printf("Enter quantity: ");
	/*calculate total amount of all quantity*/
	pItem->amount =(float)pItem->qty * pItem->price;
	/*print item details*/
	printf("\nName: %s",pItem->itemName);
	printf("\nPrice: %f",pItem->price);
	printf("\nQuantity: %d",pItem->qty);
	printf("\nTotal Amount: %f",pItem->amount);
	return 0;


Enter product name: Pen 
Enter price:5.50 
Enter quantity: 15 
Name: Pen 
Price: 5.500000 
Quantity: 15 
Total Amount: 82.500000

C Structure & Union Programs »



Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.