Pointers and Arrays in C++

Pointers and Arrays in C++


Pointers


The indirection operator dereferences a pointer, allowing code to work with the value that pointer points to.

int x = 7;
int *iptr = &x;
What will be displayed if you send the expression *iptr to cout? What happens if you send the expression ptr to cout?

The value 7 will be displayed if the expression *iptr is sent to cout. If the expression iptr is sent to cout, the address variable x will be displayed.

Three different uses for the * operator are the multiplication operator, definition of a pointer variable, and the indirection operator.

The math operations that are allowed on pointers are addition and subtraction.

When a ptr is a pointer to an int, something does happen when you add 4 to ptr. It adds 4 times the size of an int to the address stored in ptr.

int numbers [] = {2, 4, 6, 8, 10};
cout 8

The purpose of the new operator is to dynamically allocate memory.

When a program uses the new operator to allocate a block of memory but the amount of requested memory is not available, an exception is thrown which causes the program to terminate. Programs written with older compilers would handle this when the new operator returns the null address (address 0) when it cannot allocate the requested amount of memory.

The purpose of the delete operator is to free memory that has been dynamically allocated with the new operator.

The circumstances that you can successfully return a point from a function are:
You should only return a point from a function if it is:

A pointer to an object that was passed into the function as an argument and a pointer to a dynamically allocated object.

Arrays


Each byte in memory is assigned a unique address.

The address (&) operator can be used to determine a variable’s address.


Pointer variables are designed to hold addresses.


The indirection (*) operator can be used to work with the variable a pointer points to.


Array names can be used as pointers and vice versa.


Creating variables while a program is running is called dynamic memory allocation.


The new operator is used to dynamically allocate memory.


Under older compilers, if the new operator cannot allocate the amount of memory requested, it returns 0 or null.


A pointer that contains the address 0 is called a(n) null pointer.


When a program is finished with a chunk of dynamically allocated memory, it should free it with the deleter operator.


You should only use pointers with delete that were previously used with new.


Each byte of memory is assigned a unique address.


The * operator is not used to get the address of a variable.


Pointer variables are designed to hold addresses.


The & symbol is not called the indirection operator.


The & operator does not dereference a pointer.


When the indirection operator is used with a pointer variable, you are actually working with the value the pointer in pointing to.7


Array names can be dereferenced with the indirection operator.


When you add a value to a pointer, you are actually adding that number times the size of the data type referenced by the pointer.


The address operator is not needed to assign an array’s address to a pointer.


You cannot change the address that an array name points to.


Any mathematical operation, including multiplication and division, may not be performed on a pointer.


Pointers may be compared using the relational operators.


When used as function parameters, reference variables are much easier to work with than pointers.


The new operator dynamically allocates memory.


A pointer variable that has not been initialized is not called a null pointer.


The address 0 is generally considered unusable.


In using a pointer with the delete operator, it is necessary for the pointer to have been previously used with the new operator.


double value = 29.7;
double *ptr = &value;
cout statement using the ptr variable


cout

int set [10];
statement using pointer notation that stores the value 99 in set 7

*(set + 7) = 99;
code that dynamically allocates an array of 20 integers, then using a loop to allow the user to enter values for each element.

const int SIZE = 20;
int * ptr;
ptr = new int [SIZE];
for (int i =0; i
{
          cout
          cin >> ptr [i];
}
tempNumbers is a pointer that points to a dynamically allocated array. code that releases the memory.

delete [] tempNumbers;
void getNumber (int &n)

{
          cout           cin >> n;
}
the parameter n is a reference variable. rewrite the function so that n is a pointer.
void getNumber (int *n)
{
          cout
          cin >> *n;
}
definition of ptr, a pointer to a constant int.

const int *ptr;
definition of ptr, a constant pointer to an int.

int * const ptr;
int ptr* = nullptr;

The variable should be declared as int *ptr.
int x, *ptr = nullptr;

&x = ptr;
The assignment statement should read ptr = &x;
int x, *ptr = nullptr;

*ptr = &x;
The assignment statement should read ptr = &x;
int x, *ptr = nullptr;

ptr = &x;
ptr = 100;                                  // Store 100 in x
cout The assignment statement should read *ptr = 100;
int numbers [] = {10, 20, 30, 40, 50 };

cout cout > endl;
The last line should read: cout
int values [20], *iptr = nullptr;
iptr = values;
iptr *= 2;
Multiplication cannot be performed on pointers.
float level;
int fptr = &level;
An int pointer is being used to reference a float value.
int *iptr = &ivalue;
int ivalue;
iptr cannot be initialized with the address of ivalue. ivalue is defined after iptr.
void doubleVal (int val)
{
          *val *= 2;
}
The * operator is used on the parameter, but it is not a pointer.
int *pint = nullptr;
new pint;
The second statement should read pint = new int;
int *pint = nullptr;
pint = new int;
if (pint == nullptr)
         *pint = 100;
else
          cout The progrm segment is storing a value at the address 0.
int *pint = nullptr;
pint = new int [100];           // Allocate memory
delete pint                         // Free memory
The last line should read delete [] pint;
int *getNum()
{
          int wholeNum;
          cout
          cin >> wholeNum;
          return &wholeNum;
}
The function returns the address of a variable that no longer exists.
const int arr [] = {1, 2, 3};
int *ptr = arr;
The pointer definition should read: const int *ptr = array;
void doSomething (int * const ptr)
{
          int localArray [] = {1, 2, 3};
          ptr = localArray;
}
The function tries to change the contents of ptr, which is a constant pointer. During the function’s execution, the value in ptr cannot be changed.