



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth explanation of pointers in c++ programming, including their background, behavior, syntax, usage, and various types. Pointers enable more flexible data manipulation, efficient handling of complex data structures, and polymorphism. Learn how to declare, dereference, and use pointers, as well as const pointers, null pointers, uninitialized pointers, and deallocated pointers.
Typology: Slides
1 / 7
This page cannot be seen from the preview
Don't miss anything!




When you declare a variable, the computer associates the variable name with a particular location in memory and stores a value there.
When you refer to the variable by name in your code, the computer must take two steps:
C++ allows us to perform either one of these steps independently on a variable with the & and * operators:
Memory addresses, or pointers, allow us to manipulate data much more flexibly; manipulat ing the memory addresses of data can be more efficient than manipulating the data itself. Just a taste of what we’ll be able to do with pointers:
Pointers are just variables storing integers – but those integers happen to be memory ad dresses, usually addresses of other variables. A pointer that stores the address of some variable x is said to point to x. We can access the value of x by dereferencing the pointer.
As with arrays, it is often helpful to visualize pointers by using a row of adjacent cells to represent memory locations, as below. Each cell represents 1 block of memory. The dot- arrow notation indicates that ptr “points to” x – that is, the value stored in ptr is 12314, x’s memory address.
ptr x
2.2.1 Declaring Pointers
To declare a pointer variable named ptr that points to an integer variable named x:
int * ptr = & x ;
int *ptr declares the pointer to an integer value, which we are initializing to the address of x.
We can have pointers to values of any type. The general scheme for declaring pointers is:
data_type * pointer_name ; // Add "= initial_value " if applicable
pointer name is then a variable of type data type * – a “pointer to a data type value.”
2.2.2 Using Pointer Values
Once a pointer is declared, we can dereference it with the * operator to access its value:
cout << * ptr ; // Prints the value pointed to by ptr , // which in the above example would be x ’s value
We can use deferenced pointers as l-values:
Some pointers do not point to valid data; dereferencing such a pointer is a runtime error. Any pointer set to 0 is called a null pointer, and since there is no memory location 0, it is an invalid pointer. One should generally check whether a pointer is null before dereferencing it. Pointers are often set to 0 to signal that they are not currently valid. Dereferencing pointers to data that has been erased from memory also usually causes runtime errors. Example:
1 int * myFunc () { 2 int phantom = 4; 3 return & phantom ; 4 }
phantom is deallocated when myFunc exits, so the pointer the function returns is invalid. As with any other variable, the value of a pointer is undefined until it is initialized, so it may be invalid.
When we write void f(int &x) {...} and call f(y), the reference variable x becomes another name – an alias – for the value of y in memory. We can declare a reference variable locally, as well: int y ; int & x = y ; // Makes x a reference to , or alias of , y
After these declarations, changing x will change y and vice versa, because they are two names for the same thing.
References are just pointers that are dereferenced every time they are used. Just like point ers, you can pass them around, return them, set other references to them, etc. The only differences between using pointers and using references are:
The usage of the * and & operators with pointers/references can be confusing. The * operator is used in two different ways:
The name of an array is actually a pointer to the first element in the array. Writing myArray[3] tells the compiler to return the element that is 3 away from the starting el ement of myArray.
This explains why arrays are always passed by reference: passing an array is really passing a pointer.
This also explains why array indices start at 0: the first element of an array is the element that is 0 away from the start of the array.
Pointer arithmetic is a way of using subtraction and addition of pointers to move around between locations in memory, typically between array elements. Adding an integer n to a pointer produces a new pointer pointing to n positions further down in memory.
4.1.1 Pointer Step Size
Take the following code snippet:
1 2 3
long arr [] = {6 , long * ptr = arr ; ptr ++;