C++ is an extension of C programming model. Memory is a sequence of words or bytes that is accessed by an address that is indexed by integers. Modern machines support direct function call stack and input-output operations require special machine instructions or access to ‘‘memory’’ locations with peculiar semantics. Any higher-level language will find using these facilities pretty messy and machine-architecture-specific. C is by far the most successful language providing the programmer with a programming model that closely matches the machine model. C provides language-level and machine-architecture-independent notions that directly map to the key hardware notions: characters for using bytes, integers for using words, pointers for using the addressing mechanisms, functions for program abstraction, and an absence of constraining language features so that the programmer can manipulate the inevitable messy hardware-specific details.
Before moving to C++ pointers, lets understand pointers in C language. A C array is simply a sequence of memory locations accessed by integer indices. For example:
int num [10]; // an array of 10 ints
num[3] = 11; // assign 11 to num[3]
int x = num[3]; // read value in num[3] and assign to x
The subscript notation [] is used both in declarations to indicate an array and in expressions referring to elements of an array. A C pointer is a variable that stored the memory address. For example:
int *p; // p is a pointer to an int
p = &num[3]; // assign the address of num[3] to p
*p = 14 ; // write 14 to num[3] via pointer p
int ptrVal = *p ; // read from num[3] through p and assign to ptrVal
The pointer dereference (‘‘points to’’) notation * is used both in declarations to indicate a pointer and in expressions referring to the element pointed to. C++ built on C's idea of expressions, control structures, and functions. Many C++ tasks such as providing functions as call backs, are performed more easily with pointers. Things like dynamic memory allocation, cannot be performed without them. Every variable is a memory location and every memory location has its address defined that can be accessed using ampersand (&) operator which denotes an address in memory.
A pointer in short is a variable holding memory address of another variable. It can be declared as
type *variablename;
int *ptr; //pointer to an integer variable
float *fp; //pointer to a floating point number
char *pch; //pointer to a character variable
We assign memory address of a variable to a pointer using & operator and access the value at memory location stored in a pointer by * operator. The following code shows working of pointers. Save the code in a file say pointerDemo.cpp
#include <iostream>
using namespace std;
int main () {
int count = 20; // actual variable declaration.
int *iptr; // pointer variable
iptr = &count; // store address of var in pointer variable
cout << "Value of counter variable: ";
cout << count << endl;
// print the address stored in ip pointer variable
cout << "Address stored in iptr variable: ";
cout << iptr << endl;
// access the value at the address available in pointer
cout << "Value of *iptr variable: ";
cout << *iptr << endl;
return 0;
}
Now run compile and run the program
(base) 22:48:10:~ % g++ pointerDemo.cpp
(base) 22:48:12:~ % ./a.out
Value of counter variable: 20
Address stored in iptr variable: 0x7ffee14227a8
Value of *iptr variable: 20
You see the value of counter can be accessed by variable and pointer both and address of counter variable is a hexadecimal 0x7ffee14227a8. It is always a good idea to initialize a pointer variable with NULL. Null pointer is a constant with a value of zero defined in several standard libraries
Now we see how we pass pointers to a function. Save the following code in a file say pointerAsArg.cpp.
#include <iostream>
using namespace std;
int add( int *ptr1, int *ptr2);
int main ()
{
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
int *ptr1=NULL, *ptr2=NULL;
ptr1 = &num1;
ptr2 = &num2;
int result;
result = add(ptr1, ptr2);
// print the sum of num1 and num2
cout << num1 <<" + "<<num2<<" = "<<result;
cout << endl;
return 0;
}
int add( int *ptr1, int *ptr2)
{
return (*ptr1 + *ptr2);
}
Now compile the code and run it. This code takes two integers as input from user, pass them as pointers ta local function that adds them and return their sum.
(base) 22:58:15:~ % g++ pointerAsArg.cpp
(base) 22:58:20:~ % ./a.out
Enter first number: 12
Enter second number: 13
12 + 13 = 25