C++/Pointers

From Wikiversity

< C++
Jump to: navigation, search
Crystal Clear app kaddressbook.png
Please help develop this page

This page was created, but so far, little content has been added. Everyone is invited to help expand and create educational content for Wikiversity. If you need help learning how to add content, see the editing tutorial and the MediaWiki syntax reference.

To help you get started with content, we have automatically added references below to other Wikimedia Foundation projects. This will help you find materials such as information, media and quotations on which to base the development of "C++/Pointers" as an educational resource. However, please do not simply copy-and-paste large chunks from other projects. You can also use the links in the blue box to help you classify this page by subject, educational level and resource type.

Wikipedia-logo.png Run a search on C++/Pointers at Wikipedia.
Commons-logo.svg Search Wikimedia Commons for images, sounds and other media related to: C++/Pointers
Wikimedia-logo.svg Search for C++/Pointers on the following projects:
Smiley green alien whatface.svg Lost on Wikiversity? Please help by choosing project boxes to classify this resource by:

In C++, pointers are a primitive datatype that reference an object.

Contents

[edit] What is a pointer?

In short, a pointer can be thought of as an address in memory.

All the data stored in a program is stored in the computer's memory. For example, if a program has a global variable named "numberOfEmployees" that variable has both a value, for example 120 employees, and an address, which is the actual location in the computer's memory where the value is stored.


[edit] Declaring pointers

Declaring a pointer is similar to declaring a regular variable, although the name is preceded by an asterisk:

  int *ptr; 
  struct coord *pCrd; 
  void *vp;

In the example above, ptr is a pointer to an integer. pCrd is a pointer to a structure named coord. vp is a void pointer, which does not require a specific datatype.

Unlike references, pointers are not guaranteed to be initialized. As such, they should only be used when they are known to point to an existing object.

[edit] Arrays

In C++, arrays are declared similar to the following:

  int arr[25];

The above statement declares as static array of 25 elements, which can be accessed individually. Although arr behaves as a pointer, it's value cannot be changes as it references a specific region in memory.

[edit] Allocating variables

In C++, a new object, variable or array can be created using the new operator, and freed with the delete operator.

  int *ptr = new int;
  /* ... */
  delete ptr;

The new operator allocates an object from the heap and optionally initialize it. When you have finished using it, you must delete it. Otherwise, the pointed memory is inaccessible and the result is memory leak.

[edit] Referencing variables

The & operator is used to reference an object. When using this operator on an object, you are provided with a pointer to that object. This new pointer can be used as a parameter or be assigned to a variable.

[edit] Multi dimensional arrays

A multidimensional array allows nesting arrays:

  int grid[3][3];

This allocates 3*3 elements in one memory block. Even though arrays behave similarly to pointers, a multidimensional array is not a pointer-to-a-pointer, as shown below.

low address                                                                             high address
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│grid                                                                                              │
├────────────────────────────────┬────────────────────────────────┬────────────────────────────────┤
│grid[0]                         │grid[1]                         │grid[2]                         │
├──────────┬──────────┬──────────┼──────────┬──────────┬──────────┼──────────┬──────────┬──────────┤
│grid[0][0]│grid[0][1]│grid[0][2]│grid[1][0]│grid[1][1]│grid[1][2]│grid[2][0]│grid[2][1]│grid[2][2]│
└──────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┘

The objects grid, grid[0] and grid[0][0] is always at the same location (but different types) but the objects of pptr, *pptr, **pptr is at different locations. When evaluating grid[0][0], The array grid (which is an int[3][3]) is first converted to a pointer of type int(*)[3], taking the element at offset 0, and yields an object of int[3], then it is converted to int* again and the element at offset 0 is taken, generating an object of type int& .

[edit] Pointer to a pointer

A pointer contains a reference to another variable. It may also point to a pointer:

  int **pptr;
┌────┐┌─────┐┌──────┐
│pptr││*pptr││**pptr│
└─┬──┘└─┬─┬─┘└───┬──┘ 
  └─────┘ └──────┘

For pptr[0][0], the address stored in pptr is taken and the address stored in that address is taken and it is the result of the expression.

[edit] Where To Go Next

Topics in C++
Beginners Data Structures Advanced
Part of the School of Computer Science