- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Access Structure Member
In C++, once a structure variable has been defined, its member can be accessed through the use of dot (.) operator. For example, the following code fragment assigns 1740 to year element of birth_date structure variable declared earlier:
birth_date.year = 1740 ;
Syntax to Access Structure Member in C++
The structure variable name followed by a period or dot (.) and the element name references to that individual structure element. The syntax to access structure element is shown here:
structure-name.element-name
Remember that the first component or element of an expression involving the dot (.) operator is the name of specific structure variable (birth_date in this case), not the name of structure specifier (date).
The structure members are treated just like other variables. So, to print year of birth_date, you can simply write:
cout << birth_date.year ;
In same fashion, to read day, month and year of joining_date, you can simply write :
cin >> joining_date.day >> joining_date.month >> joining_date.year ;
C++ Access Structure Members Example
Here is an example, demonstrating how to access members of a structure in C++
/* C++ Access Structure Member */ #include<iostream.h> #include<conio.h> struct st { int a; // structure member int b; // structure member int sum; // structure member }st_var; // structure variable void main() { clrscr(); cout<<"Enter any two number:\n"; // accessing structure member a and b cin>>st_var.a>>st_var.b; // accessing structure member sum, a, and b st_var.sum = st_var.a + st_var.b; // accessing structure member sum cout<<"\nSum of the two number is "<<st_var.sum; getch(); }
Here is the sample run of this C++ program:

« Previous Tutorial Next Tutorial »