- Basics of C++
- Structure of a program
- Variables and types
- Basic Input/Output
A pointer is a variable that is used to store a memory address. Address is the location of the variable in the memory. Pointers help in dynamic memory allocation. Pointers improve execution time and saves space. Pointer points to particular data type. General form of declaring pointer is:-
type is base type of the pointer and variable_name is the name of the variable of the pointer. For example,
x is variable name and it is the pointer of type integer.
A computer memory location has an address and holds a content. The address is a numerical number (often expressed in hexadecimal), which is difficult for programmers to use directly. Typically, each address location holds 1 byte (i.e., 8-bit) of data. It is entirely up to the programmer to interpret the meaning of data, such as integer, real number, characters or strings.
To ease the burden of programming using numerical address and programmer-interpreted data, early programming languages (such as C) introduce the concept of variables. Variable is a named location that can store a value of a particular type. Instead of numerical addresses, names (or identifiers) are attached to certain addresses. Also, types (such as int, double, char) are associated with contents for ease of interpretation of data.
Each address location typically hold 8-bit (i.e., 1-byte) of data. A 4-byte int value occupies 4 memory locations. The 32 bit system typically uses 32-bit addresses. To store 32 bit address, 4 memory locations are required.
Following diagram illustrate relationship between computers' memory address and content; and variable's name, type and value used by the programmers.
Pointer Variables (or Pointers)
A pointer variable (or pointer in short) is basically the same as the other variables, which can store a segment of data. Unlike a normal variable which stores a value (such as int, double, char), a pointer stores a memory address.
Pointers must be declared before they can be used, just like a normal variable. Syntax of declaring a pointer is to place a * in front of the name. A pointer is associated with a type (such as int and double) too.
type * ptr; // I shall adopt this convention
int* p1, p2, i; // p1 is a int pointer, p2 and i are int
int * p1, * p2, i; // p1 and p2 are int pointers, i is an int
Initializing Pointers via Address-Of Operator (&)
When we declare a pointer variable, its content is not initialized. Or we can say, it contains an address of "somewhere", which is of course not a valid location. This is dangerous! We need to initialize a pointer by assigning it a valid address. This is normally done via address-of operator (
The address-of operator (
&) operates on a variable, and returns address of the variable. Example, if number is an int variable, &number returns address of the variable number.
We can use the address-of operator to get the address of a variable, and assign the address to a pointer variable.
int * pNumber; // Declare a pointer variable called pNumber pointing to an int (or int pointer)
pNumber = &number; // Assign the address of the variable number to pointer pNumber
int * pAnother = &number; // Declare another int pointer and init to address of the variable number
As illustrated, the int variable number, starting at the address 0x22ccec, contains int value 88. Expression &number returns the address of the variable number. This address is then assigned to pointer variable 'pNumber', as its initial value.
The address-of operator (
&) can only be used on the RHS.
Indirection or Dereferencing Operator (*)
The indirection operator (or dereferencing operator) (*) operates on a pointer, and returns value stored in the address kept in the pointer variable. Example, if pNumber is an int type pointer, *pNumber returns int value "pointed to" by pNumber.
int * pNumber = &number; // Declare and assign the address of variable number to pointer pNumber (0x22ccec)
cout << pNumber<< endl; // Print the content of the pointer variable, which contain an address (0x22ccec)
cout << *pNumber << endl; // Print the value "pointed to" by the pointer, which is an int (88)
*pNumber = 99; // Assign a value to where the pointer is pointed to, NOT to the pointer variable
cout << *pNumber << endl; // Print the new value "pointed to" by the pointer (99)
cout << number << endl; // The value of variable number changes as well (99)