new
keyword is used, returns address of the memory allocatedchar* variableName = new char;
ptr = NULL
=> Point it towards no value, Gets destroyed when exiting from stackint *variableName = new int[n];
=> Creating an 1D arrayA = new dataType[n]
=> Initialize an array of size n, A pointer stores the memory address of first elementdataType *A = new int[n * m]
=> Dynamically declare an 2D array for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
for (int k = 0; k < D; k++) {
cout << *(A + i * C * D + j * D + k);
}
}
}
int **A = new int *[R];
for (int i = 0; i < R; i++) {
A[i] = new int[C];
}
// Initialize the elements
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cin >> A[i][j];
}
}
// Print the elements
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cout << *(A + i * R + j);
}
cout << endl;
}
// Deallocate the memory
for (int i = 0; i < R; i++) {
delete[] A[i];
}
delete[] A;
delete
keyword is useddelete variableName;
delete[] variableName;
=> For array deletion