Array basics
- C
- C++
src/c/arrays-pointers/01-array-basics.c
#include <stdio.h>
int main()
{
const int SIZE = 5;
// int array1[SIZE] = {0}; // if the array size is not knowns compile time,
// use heap dynamic memory
// int array2[SIZE] = {1, 2, 3, 4, 5};
// some compiler might complain here, use 5 instead of SIZE
// fails in gcc, however works in apple clang
int array1[5] = {0};
int array2[5] = {1, 2, 3, 4, 5};
int array3[] = {1, 2, 3, 4, 5};
char str1[] = "hello";
int i;
double sum2 = 0, sum3 = 0;
double *ptr_sum2 = &sum2;
for (i = 0; i < SIZE; i++)
{
sum2 += array2[i];
sum3 += array3[i];
}
printf("Sum2 = %f\n", sum2);
printf("Sum3 = %f\n", sum3);
printf("Pointer sum2: %p\n", ptr_sum2);
printf("Sum2 = %f\n", *ptr_sum2);
printf("array2 position: %p\n", array2);
return 0;
}
src/cpp/arrays-pointers/01-array-basics.cpp
#include <iostream>
using namespace std;
void print_2d_array(float *, int, int);
int main()
{
int arr[] = {2, 4, 8, 15};
// arr[4] = {1, 2};
// above assignment sets: arr[0] = 1, arr[1] = 2, arr[2] = 0, arr[3] = 0
// aggregation initialization:
// note that if we do not provide any value via curly braces, the array
// remains uninitialized. To make sure array is initialized to zeros:
// arr[4] = {}
for (int i : arr)
{
cout << i << endl;
}
const int SIZE = 10; // `const` is necessary here
double data[SIZE];
// if the size of array is not known compile time, use heap dynamic array
cout << "Size of data = " << sizeof(data) << " bytes\n";
cout << "Length of data array: " << sizeof(data) / sizeof(data[0]) << endl;
// the above prints the size of the entire array
// multi-dimensional array:
float arr2d[3][4] = {0.0}; // this initializes all elements to with 0.0
cout << "\narr2d:\n";
print_2d_array(&arr2d[0][0], 3, 4);
float arr2d2[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
cout << "\narr2d2:\n";
print_2d_array(&arr2d2[0][0], 3, 4);
float arr2d3[3][4] = {{1.1, 2.2, 3.3, 4.4},
{5.5, 6.6, 7.7, 8.8},
{9.9, 10., 11., 12.}};
cout << "\narr2d3:\n";
print_2d_array(&arr2d3[0][0], 3, 4);
float arr2d4[][4] = {{1.1, 2.2, 3.3, 4.4},
{5.5, 6.6, 7.7, 8.8},
{9.9, 10., 11., 12.}};
cout << "\narr2d4:\n";
print_2d_array(&arr2d4[0][0], 3, 4);
float arr2d5[][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
cout << "\narr2d5:\n";
print_2d_array(&arr2d5[0][0], 3, 4);
float arr2d6[3][4] = {0.3}; // sets first element to 0.3, rest to 0.0
cout << "\narr2d6:\n";
print_2d_array(&arr2d6[0][0], 3, 4);
return 0;
}
void print_2d_array(float *arr, int m, int n)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
// array elements are continuously stored row by row in C++
cout << *(arr + (n * i) + j) << "\t";
// | --- pointer to the first element, do the pointer
// | arithmetic to get the next elements
// dereference the value
}
cout << endl;
}
}