Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 3.10: Pointer Types

Adapted from: “Object-Oriented Programming Using C++” by Ira Pohl (Addison- Wesley)

Program that demonstrates pointer types in C++

// Pointer - based call - by - reference
#include <iostream>

using namespace std;

void order (int *, int *);

int main()
{
    int i = 7, j = 3;

    cout << i << '\t' << j << endl; // 7 3 is printed
    order(&i, &j);
    cout << i << '\t' << j << endl; // 3 7 is printed
}

void order(int * p, int * q)
{
    int temp;

    if (*p > *q) {
        temp = *p;
        *p = *q;
        *q = temp;
    }
}

Compile and Execute the Program

The above program is compiled and run using Gnu Compiler Collection (g++):

import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Cpp_Code/Chapter_3_10_Pointer_Types"
ch_dir_stat = os.chdir(code_dir)
build_command = os.system("g++ call_ref.cpp -w -o call_ref")
exec_status = os.system("./call_ref")
7	3
3	7