C++ Pointers
June 11, 2020 by Jane

Declaration:

int * a, * b; // notice that to declare two pointers, there are two asterisks

int * a,  b; // a is an integer pointer but b is an integer

 

Array/pointer duality law:

a[n] == *(a+n)

 

Common Uses:

Dynamically allocating memory on the heap because the size is unknown

To extend life-time of a variable to be used in other parts of the program

 

Function pointers: it's possible to pass functions as parameters to other functions like higher order functions. 

return_type (*func param name)(input param type, input param2 type...)

E.g. a function being passed to print_table(): double is the return type, f is the function parameter name, and the function takes a single double as input

void print_table(double (*f)(double)) { 

    cout << setprecision(2);
    for (double x = 1; x <= 10; x++)
    {
        double y = f(x)
// the function is called here
        cout << setw(10) << x << "|" << setw(10) << y << endl;
    }

}

 

Const Pointers and Declaration:

Pointers can be declared with const, it's often one of the confusing topics of C++, what const means in different places in the declaration expression. A tip is to read the declarations from right to left.

A good explanation can be found here: https://isocpp.org/wiki/faq/const-correctness

 

E.g. 1.

const [type] [var];

const int a; // integer a is constant, must be initialized with a value which cannot be changed

const Obj o; // object o is a constant, can only be accessed with const accessors.

 

E.g. 2.

const [type] * [var];

const int * p; // p is a pointer to an integer that is constant; p can point to other address, but cannot be dereferenced and reassigned

int a = 2; int b = 3;

const int * p = &a; // OK

p = &b; // OK

*p = 3; // not allowed

 

E.g. 3. same as E.g. 2, it's an alternative version

[type] const * [var];

int const * q; // q is a pointer to a constant integer; q can point to other address, but cannot be dereferenced and reassigned

int a = 2; int b = 3;

int const * q = &b; // OK

q = &a; // OK

*q = 4; // not allowed

 

E.g. 4. opposite situation as E.g. 2 and 3, pointer cannot change, value can be dereferenced.

[type] * const [var];

int * const m; // m is a constant pointer to an integer, m can be dereferenced and reassigned another int but it cannot point to a different address

int a = 2; int b = 3;

int * const m = &a; // OK

*m = 7; // OK

m = &b; // not allowed

 

const int c = 3;

int * const n = &c; // not allowed because c cannot be changed yet if we do *n = 8 it will get changed. Compiler doesn't allow int * const type variables to be initialized by const int * variables.

 

E.g. 5. nothing can change, everything is const

const [type] * const [var];

const int * const r; // r is a constant pointer to a constant integer, cannot reassign to different address, cannot dereference and assign a different integer.

int a = 2; int b = 3;

const int * const r = &a; // OK

*r = 7; // not allowed

r = &b; // not allowed