Menu

01_Pointers References

Nitin Deshpande

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?

  • How to pass references as parameters to functions?
  • Return values as references from functions?
  • Pass by ref vs pass by value...

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]:
{

  • C: Do not confuse with the address operator "&".... this merely returns the address of the object.
  • C: The reference &refT is a different way to express T. Operations such as refT++, refT--, refT*=4 affect the value of T.
  • C: The reference & occurs only in the declaration/inits.
    }
    =======================================================================================================
    http://www.cplusplus.com/reference/string/string/getline/
    std::getline (string)
    (1)
    istream& getline (istream& is, string& str, char delim);
    istream& getline (istream&& is, string& str, char delim);
    (2)
    istream& getline (istream& is, string& str);
    istream& getline (istream&& is, string& str);
    =======================================================================================================
    Parameters
    is
    istream object from which characters are extracted.
    str
    string object where the extracted line is stored.
    The contents in the string before the call (if any) are discarded and replaced by the extracted line.
    =======================================================================================================
    Return Value
    The same as parameter is.
    =======================================================================================================
    cout<< "Enter the name, once done, hit Enter..." << endl;
    getline(cin,name); //http://www.cplusplus.com/reference/string/string/getline/
    cout<< "name contains: "<< name << endl;
    =======================================================================================================

Functions that return reference.
C[2]:
{

  • C: <type>& functionname(param1,param2,...,paramN)
    eg: float& floorval(float floatVal) returns the floor value of a float value number. For instance, floorval(4.98) would return 4.0F.</type>
  • C: Some operation performed on the function would effect the return value and not the parameters. ++floorval(4.98) would return 5.0F.
  • C: Returning ref from a function vs normal functions - In the latter case, you cannot perform any operations on the return values of the function call, but in the former case it
    is possible. For eg: float floorval(float floatVal), this operation during function call ++floorval(1.0,4.0) is not possible.
  • C: <type>& functionname(param1,param2,...,paramN) returns an object of type <type>
    }</type></type>

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