#include /* * global variable; can be used in C++, but is considered poor style * unless it is at least encapsulated in a namespace (not done here). */ constexpr int N = 7; int main() { int cubes[N]{}; // curly braces initialize to default; here, zeroes // above, we could also simply have written: int vector[7]{}; for(int i = 0; i < N; i++) cubes[i] = i*i*i; std::cout << "cubes[3] evaluates to " << cubes[3] << ".\n\n"; std::cout << "cubes evaluates to " << cubes << ".\n"; std::cout << "cubes + 3 evaluates to " << cubes + 3 << ".\n\n"; std::cout << "*cubes accesses the value at the address cubes; " << "that is " << *cubes << ".\n"; std::cout << "*(cubes+3) accesses the value at the address cubes+3; " << "that is " << *(cubes + 3) << ".\n\n"; /* * you could safely do the below in Python, but not in C++ * it is a serious mistake - never do this!!! */ std::cout << "Attention. Never do the following.\n" << "cubes[-1] evaluates to " << cubes[-1] << ".\n" << "*(cubes-1) evaluates to " << *(cubes - 1) << ".\n\n"; std::cout << "That was the content that happened to be in memory " << "right before the array, at the address " << cubes-1 << ".\n" << "Accessing memory in this way is not simply poor style, " << "it can even cause the program to fail (segmentation fault).\n"; }