01 Pointers and references
This readme would contain notes and practical aspects from
[CGCPP] A_Complete_Guide_to_Programming_in_C++_Ulla_Kirch_Prinz_Peter_Prinz
[ISCPP] Industrial_Strength_C++_Henricson_Nyquist
C[<number>]</number>: Important concepts
C: Concept
MAIN: Main objective / Introductory notes.
[CGCPP]: References and pointers
Objectives of the chapter:
MAIN: What is a pointer and what is a reference?
MAIN:
A reference is a mere alias for an existing object. Meaning, it would store the address of a variable.
C: A reference value has to be initialised in the code. If not, trash values can occur, or the compiler will generate
an error message.
C: Once initialised, it is unchangeable. Meaning, in the subsequent lines of codes, you cannot use the reference to address a different variable.
C: & is used to define a reference to a datatype. for eg: float &p; is read as reference to type float.
C: Reference provided to an object should be of the same type. Meaning, the following lines of code will not compile:
int h=10;
float &p=h;
C: Underneath it all, a reference refT to object T is typically the machine address of the object T.
C: In compiler writer lingo, a reference is an "lvalue" (something that can appear on the left hand side of an assignment operator).
C[1]:
{
Functions that return reference.
C[2]:
{
C[3]:
{
C: Stream Class Shift Operators "<<" and ">>"
C: << returns reference to cout object.
C: >> returns reference to cin object.
}
C[4]:
{
C: Assignment Operator =
The assignment operator '=' returns a reference to the lvalue (operand on the left of the '='). The lvalue must therefore be an object.
}
C[5]:
{
Pointers
C: A Pointer is an expression that contains the data and address of another object. The & operator provides the address to an int object, and pointer to a declared object.
C: A pointer points to an address of an object.
C: Pointer variables can store address of another object. Init details int ptr or int ptr ( read as ptr is a pointer to type int, * to be read as pointer to).
C: Declare a pointer of type T, T* ptrName;, next, point the pointer at a memory address (of a variable that belongs to the same base class as ptrName, i.e T.) of another variable.
T varName; ptrName = &varName;
Pointers vs References
C: A pointer is not merely an alias. However, both references and pointers refer to an object in memory.
C: A pointer has a separate identity of its own. It has its address. T* ptrName; &ptrName would provide the address of the pointer ptrName.
C: A reference on the other hand cannot be manipulated. In contrast to a reference, a pointer can be manipulated by pointing it to a new address in memory.
The Dereferencing operator * or Indirection operator
C: The * operator facilitates by providing the “value pointed by” a pointer ptrname.
}
C[6]:
{
Pointers as function parameters
C: A way of passing the address of the object as a function parameter.
C: This can be acheived by declaring a pointer variable.
}
Nitin Deshpande
deshpand@in.tum.de