Using new to Create Dynamic Arrays

Allocating the array during compile time is called static binding,meaing that the array is built in to the program at cimpile.But with new ,you can create an array during runtime if you need it and skip creating the array if you don't need it.
You can select an array size after the program is running.This is called dynamic binding,meaning that the array is created while the program is running.Such an array is called a dynamic array.
With static binding,you must specify the array size when you write the program.With dynamic binding,the program can decide on an array size while the program runs.
If you need an array of 10 ints,you use this:ios

int * psome = new int [10]; //get a block of 10 ints
//The new operator  returns the address of the first element of the block.In this example,
//that value is assigned to the pointer psome.

A example:express

#include<iostream>
using namespace std;

int main(void)
{
	double * p3 = new double [3];
	p3[0] = 0.2;
	p3[1] = 0.5;
	p3[2] = 0.8;

	cout<<"p3[1] is"<<p3[1]<<".\n";
	p3 = p3+1;
	cout <<"Now p3[0] is"<<p3[0]<<"and";
	cout<<"p3[1] is"<<p3[1]<<".\n";
	p3 = p3 - 1;
	delete [] p3;
	return 0;
}

Here is the output from the program:ide

p3[1] is 0.5.
Now p3[0] is 0.5 and p3[1] is 0.8.

You can't change the value of an array name.But a pointer is a variable,hence you can change its value.Note the effect of adding 1 to p3.The expression p3[0] now refers to the former second element of the array.Thus,adding 1 to p3 causes it to point to the second element instead of the first.Substracting one takes the pointer back to its riginal value so that the program can provide delete[] with the correct address.
The actual address of consecutive ints typically differ by 2 or 4 bytes,so the fact that adding 1 to p3 gives the address of the next element suggests that there is something special about pointer arithmetic.There is.ui

Note

Adding one to a pointer variable increases its value by the number of bytes of the type to which it points.this