array initialization in cpp code example
Example 1: initialize an array in c++
int nums[100] = {0};
int nums[5] = {1,2,3,4,5};
Example 2: c++ initialise array
int nCount[] = {1, 2, 3, 4, 5};
Example 3: array declaration c++
int foo [5];
Example 4: arrays in c++
#include <iostream>
#include <array>
int main()
{
int example[5];
int* another = new int[5];
delete[] another;
example[0] = 1;
example[1] = 2;
example[2] = 3;
example[3] = 4;
for (int i = 0; i < 5; i++) {
example[i] = 2;
}
int* ptr = example;
example[2] = 5;
*(ptr + 2) = 6;
std::cout << example[2] << std::endl;
*(int*)((char*)ptr + 8) = 8;
std::cout << example[2] << std::endl;
std::array<int,5> stda;
std::cout << stda.size() << std::endl;
std::cin.get();
}
Example 5: c++ initialize array
int arr[3] = {1, 5, 4};
Example 6: how to initialize array with new in c++
int* a = NULL;
int n;
cin >> n;
a = new int[n];
for (int i=0; i<n; i++) {
a[i] = 0;
}
. . .
delete [] a;
a = NULL;