Basic example using Structure and Pointer in C++


This program defines a temp struct that has two members: f and i. It then creates an instance of this struct: t. In main(), a pointer ptr of type temp and t is created. The ptr is assigned the address of t using the address-of operator &ab;.

The program then prints the values of f and i using the pointer ptr and the dereference operator *. The dereference operator is used in combination with the dot operator . to access the members of the struct pointed to by ptr. Overall, this program is an example of how to define and use a pointer to a struct in C++. It creates an instance of the temp struct, creates a pointer to this struct, assigns the address of the struct to the pointer, and then uses the pointer to access and print the values of the struct members

Source Code

#include<iostream>
using namespace std;
struct temp
{
   float f=23.34f;
   int i=10;
};
int main()
{
   temp *ptr, t;
   ptr = &t;
   cout<<"F :"<<(*ptr).f<<endl;
   cout<<"I :"<<(*ptr).i<<endl;
   return 0;
}
 
To download raw file Click Here

Output

F :23.34
I :10

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples