0% found this document useful (0 votes)
16 views27 pages

C++ References and Pointers Explained

c++ tutorial
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views27 pages

C++ References and Pointers Explained

c++ tutorial
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

References (alias) in C++, Parameter Passing

CSC 404
Complex Networks
Object Oriented Programming
Research Group
Definition of(CNeRG),
alias:
Department
otherwise called : of Computer
otherwise known as —
Science and Engineering
used to indicate an additional name that a
person (such as a criminal) sometimes uses Subrata Nandi
John Smith alias Richard Jones was identified
Dept. of Computer Science & Engineering
as the suspect Team Members: NIT Durgapur
INDIAN
National INSTITUTE
Institute OF
of Technology Sandip Chakraborty, Niloy Ganguly,
TECHNOLOGY
Durgapur
KHARAGPUR
Basabdatta Palit, Nibir Pal, Somesh
Indian Institute of Technology Kharagpur Khandelia
Outline
• Basics of Pointers
• Pointer Variables – use of & and * operator
• Multiple indirections
• Legal & Illegal Operations on Pointers

• Constants in C++
• Pointers vs Constants
• Arrays vs Pointers

• Memory Layout of C++ program

• Dynamic memory allocation in C++ - use of new and delete operators


• Dynamic allocation of 1-D arrays
• Dynamic allocation of 2-D arrays

• Reference variables
• Use of reference variables
• Reference vs constants

• Function Pointers

• Pointer to structures – self-referential structures


• Pointers and Class (objects, member methods, member data)
• Institute
Indian this pointer
of Technology Kharagpur
Outline
• Basics of Pointers
• Pointer Variables – use of & and * operator
• Multiple indirections
• Legal & Illegal Operations on Pointers

• Constants in C++
• Pointers vs Constants
• Arrays vs Pointers

• Memory Layout of C++ program

• Dynamic memory allocation in C++ - use of new and delete operators


• Dynamic allocation of 1-D arrays
• Dynamic allocation of 2-D arrays

• Reference variables
• Use of reference variables
• Reference vs constants

• Function pointers

• Pointer to structures – self-referential structures


• Pointers and Class (objects, member methods, member data)
• Institute
Indian this pointer
of Technology Kharagpur
Pseudonym

Pseudonyms include stage-names(actors) and user names, ring names, pen-names(writers), nicknames,
aliases, superhero/villain identities(actors) and code names(hackers), gamer identifications

A pseudonym or alias is a fictitious name that a person or group assumes for a particular purpose, which
differs from their original or true name (orthonym)

Objective to remain anonymous

Pseudonyms are "part-time" names, used only in certain contexts


• to provide a more clear-cut separation between one's private and professional lives,
• to showcase or enhance a particular persona,
• or to hide an individual's real identity

In programming language a pseudonym is created by using the feature called ‘reference’

Indian Institute of Technology Kharagpur


References in C++
• A reference is a feature that allows a programmer to provide an
additional name to an existing data item (variable/constant)

• If you think of a variable name as a label attached to the x

2
variable’s location in memory then you may consider reference
to be another label attached to that memory location

• One may access the location either by the original name or by


it’s reference

int x=2; // original name label ‘x’

Indian Institute of Technology Kharagpur


References in C++
• A reference is a feature that allows a programmer to provide an
additional name to an existing data item (variable/constant)
y
• If you think of a variable name as a label attached to the
x
variable’s location in memory then you may consider reference

2
to be another label attached to that memory location 2002

• One may access the location either by the original name or by


it’s reference

int x=2; // original name label ‘x’

int &y=x; // additional name (alias) label ‘y’

Indian Institute of Technology Kharagpur


References in C++
• A reference is a feature that allows a programmer to provide an
additional name to an existing data item (variable/constant)
y
• If you think of a variable name as a label attached to the
x
variable’s location in memory then you may consider reference

2
to be another label attached to that memory location 2002
z
• One may access the location either by the original name or by
it’s reference

int x=2; // original name label ‘x’

int &y=x; // additional name (alias) label ‘y’

int &z=y; // additional name (alias) label ‘y’

cout<<x<<y<<z<<endl;

Indian Institute of Technology Kharagpur


References in C++
• A reference is a feature that allows a programmer to provide an
additional name to an existing data item (variable/constant)
y
• If you think of a variable name as a label attached to the
x
variable’s location in memory then you may consider reference

3
to be another label attached to that memory location 2002
z
• One may access the location either by the original name or by
it’s reference

Symbol table
int x=2; // original name label ‘x’ Symb Type Scope Address Dimension Size (in
ol (behaves bytes)
int &y=x; // additional name (alias) label ‘y’ name as)
x,y,z int local 2002 1 2
int &z=y; // additional name (alias) label ‘y’

cout<<x<<y<<z<<endl;

y++;
cout<<x<<y<<z<<endl;

Indian Institute of Technology Kharagpur


References in C++
• One can create multiple references of an existing variable

• One can create reference of a particular type y


x
• A reference must be initialized when declared


Once a reference name is assigned it cannot be used by others

Creating reference does not create additional space in RAM


z
3
• NULL references are not possible; ref must be connected to a legitimate piece of storage

• Dynamic allocation to a reference var is NOT POSSIBLE

• Creating an array of references is NOT POSSIBLE

int &a[10];

• int &a[4]={2,3,4,5}; // error: array of references not possible

• int &a=3; // error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’

Indian Institute of Technology Kharagpur


Indian Institute of Technology Kharagpur
References in C++

Indian Institute of Technology Kharagpur


References in C++

Indian Institute of Technology Kharagpur


Passing
References

Indian Institute of Technology Kharagpur


Returning
References

Indian Institute of Technology Kharagpur


Demo Code: References

Indian Institute of Technology Kharagpur


References in C++ (References &
Constants)

Indian Institute of Technology Kharagpur


5_Demo_References_passing_returning.cpp

Indian Institute of Technology Kharagpur


5_Demo_References_passing_returning.cpp

Indian Institute of Technology Kharagpur


References in C++ (Summary)

Indian Institute of Technology Kharagpur


Function Pointers

Indian Institute of Technology Kharagpur


Indian Institute of Technology Kharagpur
Function Pointers…

Indian Institute of Technology Kharagpur


Lambda Function

Indian Institute of Technology Kharagpur


Pointers-References-Structures-Self-referential structures

Indian Institute of Technology Kharagpur


Indian Institute of Technology Kharagpur
Indian Institute of Technology Kharagpur
Indian Institute of Technology Kharagpur

You might also like