• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Pointer » How to Declaring and Accessing Structure Variables in c++
Next →
← Prev

How to Declaring and Accessing Structure Variables in c++

By Dinesh Thakur

A structure can be used in a program only if memory is allocated to it. The structure definition informs the compiler about the type and the number of data members in the structures. However, it does not allocate any memory for the structure. To use a structure in a program efficiently, a structure variable needs to be declared. The syntax for declaring a structure variable is

structure_name structure_variable;

For example, the two structure variables record1 and record2 for the structure sales_record can be declared as

sales_record record1,record2;

Instead of using a separate declaration statement, the structure variables can also be declared at the time of structure definition. To understand this concept, consider

Example , which is modified as shown here.

struct sales_record {
   //same as
}record1,record2;

Here, the structure variables record1, record2 are declared along with structure definition.

Note that providing the structure name or tag while defining it is optional. However, in this case, the structure variables can be declared only with the structure definition and not in the program. That is, while defining the structure, either the structure name or the variable declaration can be omitted but not both.

When a structure variable is declared, the C++ compiler automatically allocates sufficient memory for accommodating all the members of the structure variable. For example, when the structure variable record1 of structure sales_record is declared, the compiler allocates 9 bytes (1 byte for grade, 2 bytes for item_code, 2 bytes for item_quantity and 4 bytes for item_rate) of memory space to record1.Memory representation of RecordNote that each structure variable contains its own copy of structure members and all the structure members occupy adjacent memory locations.

After declaring structure variables, the next step is to initialize structure variables. Like arrays, a structure variable can also be initialized at the time of its declaration. The syntax for initializing a structure variable is

structure_name structure_variable = {value1,value2,…… ,value n};

The values are assigned in the order in which they are listed. That is the first value is assigned to the first member, second value to the second member and so on. For example, the members of the structure variable record1 of the structure type sales_record can be declared and initialized with the help of this statement.

sales_record record1= {'A' ,10,5,23.52};

Note that if the structure variable record1 is declared along with structure definition, then it can be initialized as shown here.

struct sales_record {
  //same as in Example above
}record1={'A',10,5,23.52}

We’ll be covering the following topics in this tutorial:

  • Accessing Structure Members
  • Structure Assignment

Accessing Structure Members

Once a structure variable is declared and initialized, the individual structure members can be accessed with the help of dot operator(‘ . ‘). The syntax for accessing the structure members is

structure_variable.member_name

To understand the concept of accessing structure members, consider these statements

cout<<record1.grade; // To display grade value of structure
// variable record1
record1.item_code=101;//Assigning value to structure member
// item_code of record1

In these statements, the members of record1 are accessed with the help of the dot operator.

The basics of structures can be summarized in a single program as shown in this example.

Example : A program to define a structure stud, declare and initialize its variable with some values and then display the values

The output of the program is

Values of stud1 are:

Roll no.: 23

Marks: 85

Result: pass

In this example, a structure stud is defined with members rollno, marks and result. Then the structure variable studl is declared and initialized at the same time. The members of studl are accessed with the help of the dot operator and their values are displayed.

Structure Assignment

The values of structure members of one structure variable can be assigned to the corresponding members of another structure variable using a single assignment statement. However, separate assignment statements are used in case of assigning values to only some structure members. Note that structure variables must belong to the same structure type. If an attempt is made to-assign a variable of one structure type to a variable of another structure type, a compile-time error is generated.

#include<iostream>
using namespace std;
struct stud //structure definition {
int rollno;
int marks;
char result[5]; //array within structure
} ;
int main() {
 stud studl={23, 85, "pass"}; //declaring and initializing
  //structure variable
  cout<<endl<<"Values of studl are: "<<endl;
  cout<< "Roll no.: "<<studl.rollno<<endl;
  cout<<"Marks: "<<studl.marks<<endl;
  cout<<"Result: "<<studl.result<<endl;
  return 0;

To understand the concept of structure assignment, consider this example.

Example : A code segment to demonstrate structure assignment

struct sales_record {
 //same as
} ;
sales_record record1 = {'A',l0,5,23.52}; // declaration and initialization
sales_record record2 ; // declaring another structure variable
record2 = record1 ; // structure assignment

In this example, a structure variable record1 is assigned to another structure variable record2 of same structure type sales_record. The values of structure members of record1 are assigned to the corresponding members of record2.

You’ll also like:

  1. How to Declaring Nested Structures in c++
  2. How to Declaring Array of Structures in c++
  3. difference between declaring a variable in the General Declaration section of a standard code module and declaring variable in the general declaration section of a form code module?
  4. Declaring and Allocating Arrays in Java
  5. Swap Variables in a Different Way Without Using Third Variables
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW