Todayโ€™s Outline


  • Pointer Arithmetic
  • Pointer Array vs Array Pointer
  • Pointer of Pointer & Pointer Reference
  • Dynamic Memory Allocation

Pointer Arithmetic


#include <iostream>
 
using namespace std;
 
int main() {
    int a[6] = {0, 1, 2, 3, 4, 5};
    int *pa = &a[1];
    cout << hex << pa << endl;   // 0x16dda72c4
    cout << hex << ++pa << endl; // 0x16dda72c8
    cout << &a[5] - pa << endl;  // 3, distance between a[5] and a[2]
 
    long b[6] = {5, 4, 3, 2, 1, 0};
    long *pb = &b[1];
    cout << hex << pb << endl;   // 0x16dda7298
    cout << hex << ++pb << endl; // 0x16dda72a0
    cout << &b[4] - pb << endl;  // 2, distance between b[4] and b[2]
 
    char str[] = "Hello World";
    char *p = str;
    cout << p + 6 << endl;       // World
    cout << &str[6] << endl;     // World
    cout << str[2] << endl;      // l
    cout << *(p + 2) << endl;    // l
    return 0;
}

Pointer Array vs Array Pointer


Pointer Array

#include <iostream>
 
using namespace std;
 
int main() {
    int a[6] = {0, 1, 2, 3, 4, 5};
    int *m[2] = {&a[0], &a[3]};
    for (int row = 0; row < 2; row++) {
        for (int col = 0; col < 3; col++)
            cout << m[row][col] << " ";
        cout << "\\n";
    }
    return 0;
}

Array Pointer

#include <iostream>
 
using namespace std;
 
void foo(int (*x)[3]) {
    cout << *(*(x + 1) + 2) << endl; // p[1][2]
    cout << *(x[2] + 1) << endl;     // p[2][1]
}
 
int main() {
    int arr[3][3] = {{1, 2, 3},
                     {4, 5, 6},
                     {7, 8, 9}};
    foo(arr); // passing array pointer to function
 
    int (*x)[3] = arr;
    cout << *(*(x + 1) + 2) << endl; // p[1][2]
    cout << *(x[2] + 1) << endl;     // p[2][1]
    return 0;
}

Pointer of Pointer & Pointer Reference


#include <iostream>
 
using namespace std;
 
void skipSpaces(char *p) {
    while (*p == ' ')
        p++;
    cout << "Address: " << &p << " | Result: " << p << endl;
}
 
void pointerOfPointer(char **p) {
    while (**p == ' ')
        (*p)++;
    cout << "Address: " << &(*p) << " | Result: " << *p << endl;
}
 
void pointerReference(char *&p) {
    while (*p == ' ')
        p++;
    cout << "Address: " << &p << " | Result: " << p << endl;
}
 
int main() {
    char str[] = "    hello";
    char *p = str;
    skipSpaces(p);        // Address: 0x16ce23288 | Result: hello
    cout << p << endl;    // Address: 0x16ce232c8 | Result:     hello
    pointerOfPointer(&p); // Address: 0x16ce232c8 | Result: hello
    pointerReference(p);  // Address: 0x16ce232c8 | Result: hello
    cout << p << endl;    // Address: 0x16ce232c8 | Result: hello
    return 0;
}

Dynamic Memory Allocation


#include <iostream>
 
using namespace std;
 
int main() {
    // Declaration
    int *p0 = new int(10);
    char *p1 = new char('a');
 
    delete p0; // free the memory pointed by p0
    delete p1; // free the memory pointed by p1
 
    // *p0 = 10;  // Illegal after deletion
 
    // Declaration
    int n;
    cin >> n;
    int *ap0 = new int[n];
    char *ap1 = new char[n];
 
    delete[] ap0; // free the memory pointed by ap0
    delete[] ap1; // free the memory pointed by ap1
 
    return 0;
}