Structure instance creation using pointer based notation – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#2911 siteicon   siteicon   siteicon  

Structure instance creation using pointer based notation

A system of creating structure instances using pointer based notation or the method indicating the address location of the structure.

Learning Objectives

  • Using the two varieties of pointer notation for declaring the structure instances.

Source Code

TC++ #2911


Source Code

Run Output

Date 1= 11-11-2011
Date 2= 12-12-2012

Code Understanding

struct Date { int day; int month; int year;};
Three integer variables day, month and year are declared in this structure.

Date *d1=new Date();
This is the pointer based declaration with *d1 marked as location of structure and new Date() on the right side completes the instance creation like a constructor is called.

Date *d2,d; d2=&d;
This is another style where *d2 has been declared along with an instance d. Then d2=&d completes the instance creation as the address of d is assigned  to the pointer. This actually creates two instances pointing to same location d2 and d.

using the -> type of symbol the pointer type structure instance is accessed. This way we can read or write the instance variable based on the type of variable.

Since d2 is also a pointer type for structure we shall again use the -> operator to access its variables.

cout<<“Date 1= “<<d1->day<<“-“<<d1->month<<“-“<<d1->year<<endl;
The address d1 to structure is being printed here again by -> notation.

cout<<“Date 2= “<<<<“-“<<d2->month<<“-“<<d2->year<<endl;
In this printing there is a variation since structure instance d is a normal instance it has been accessed using the . operator as and other instance d2 is a pointer notation is they are accessed using -> operator.


  • Pointer to structures can serve a useful purpose while passing structure to a function as a reference.

Common Errors

  • If a structure pointer only is declared, then the instance is not created unless it gets the address value assignment to an instance.

Suggested Filename(s): strupnt.cpp


sunmitra| Created: 22-Jan-2018 | Updated: 22-Jan-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues