Pointers C++

Pointers C++

What are pointers? A pointer in C++ is a variable that holds a memory address. This address is the location of another object (typically another variable) in memory. For example, if one variable contains the address of another variable, the first variable is said to point to the second.

Pointers in C/C++

Pointers in C/C++

Pointers Variables C++

A pointers declaration consists of a base type, an *, and the variable name.

The general form for declaring a pointer variable is

type *name;

To declare a pointer variable named ptr that points to an integer variable named x:

int *p;

There are two special pointer operators: * and &. The & is a unary operator that returns the memory address of its operand.  For example:

We can have pointers to values of any type.

Pointers C++

The general scheme for declaring pointers C++

//pointer name is then a variable
//of type data type * – a “pointer
//to a data type value.”data_type *pointer_name;

The value of p is the address . The value in the memory cell that p is addressing at is *p. The expression *p represents the memory cell to which p points.. To place the address of a variable into a pointer variable, you can use The address-of operator & p = &x;

Using const Pointers in C++

There are two places the const keyword can be placed within a pointer variable declaration.
This is because there are two different variables whose values you might want to forbid
changing: the pointer itself and the value it points to.

const int *ptr; declares a changeable pointer to a constant integer. The integer value cannot be changed through this pointer, but the pointer may be changed to point to a different constant integer. int * const ptr; declares a constant pointer to changeable integer data. The integer value can be changed through this pointer, but the pointer may not be changed to point to a different constant integer. const int * const ptr; forbids changing either the address ptr contains or the value it points to.

I’m lost. Where should I place * and & operators using pointers in C++. The usage of the * and & operators with pointers/references can be confusing. The * operator is used in two different ways:

  1. When declaring a pointer, * is placed before the variable name to indicate that the variable being declared is a pointer – say, a pointer to an int or char, not an int or char value.
  2. When using a pointer that has been set to point to some value, * is placed before the pointer name to dereference it – to access or set the value it points to.

A similar distinction exists for &, which can be used either

  1. to indicate a reference data type (as in int &x;), or
  2. to take the address of a variable (as in int *ptr = &x;).

 The new operator in C++

Dynamic allocation of a memory cell that can contain an integer If the operator new cannot allocate memory, it throws the exception

 p = new int; 

The delete operator

The delete operator returns dynamically allocated memory to the system for reuse, and leaves the variable’s contents undefined delete p;
A pointer to a deallocated memory (*p) cell is possible and dangerous p = NULL; // safeguard.

Forgetting to delete generates memory leaks!

Why should we use pointers in C++?

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.

What we’ll be able to do with pointers:

  1. More flexible pass-by-reference
  2. Manipulate complex data structures efficiently, even if their data is scattered in differ­ent memory locations
  3. Use polymorphism – calling functions on data without knowing exactly what kind of data it is.
  4. Some examples on Pointers in C++

Source code examples Pointers in C++


//Declare pointers p,q
int *p, *q;

// Allocate a cell of type int.
p = new int;

// Assign a value to the new cell.
*p = 1;

// Allocate a cell of type int.
q = new int;

// Assign a value to the new cell.
*q = 2;

// Output line contains: 1 2
//These values are in the cells to which p and q point.
cout << *p << " "
 << *q << endl;

// The value in the cell to which
// q points, 2 in this case, and 3
// are added together. The result is
// assigned to the cell to which p points.
*p = *q + 3;

// Output line contains: 5 2
cout << *p << " "
 << *q << endl;

// p now points to the same cell as q.
// The cell p formerly pointed to is
// lost; it cannot be referenced.
p = q;

// Output line contains: 2 2
cout << *p << " "
 << *q << endl;

// The cell to which p points (which
// is also the cell to which q points)
// now contains the value 7.
*p = 7;

// Output line contains: 7 7
cout << *p << " "
 << *q << endl;

// This changes what p points to,
// but not what q points to.
p = new int;

// Return to the system the cell to
// which p points.
delete p;

// Set p to NULL, a good practice
// following delete.
p = NULL;

// The cell to which q previously
// pointed is now lost. You cannot
// reference it.
q = NULL;

 

Example using pointers and arrays in C++

 pointers and arrays in C++

int arrayX[]={5,20,10,8,40,90};
int *p; p=&(x[3]);

Recommended posts: Dynamic Memory Allocation in C++

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com
%d bloggers like this: