A reference to the beginning of consecutive memory of a given type
int numbers[] = {32, 45, 12, 39, 7, 15};
Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
Value | 32 | 45 | 12 | 39 | 7 | 15 |
Address | 0x7fc8c013e0 | 0x7fc8c013e4 | 0x...e8 | 0x...ec | 0x...f0 | 0x...f4 |
The index is the offset of the referenced memory.
The first element has index 0.
0 means that there is no offset from the beginning of the referenced memory.
For most C++ programmers, C-arrays only have theoretical
value for understanding how vector
works. In practice,
std::vector
should be used almost always instead of C-arrays.
Subscription is the selection of an element using its index.
int points[] = {32, 45, 12, 39, 7, 15};
int value = points[2]; // access third element with the subscript operator
points[0] = 50; // write to first element with the subscript operator
#include <iostream>
int main() {
unsigned int numbers[3]; // number of elements known at compile time
numbers[0] = 15;
numbers[2] = 5;
numbers[1] = 10;
std::cout << numbers[0] << " " << numbers[1] << " " << numbers[2] << "\n";
return 0;
}
The compiler can determine the number of elements if the array was declared in the current scope.
The sizeof
operator returns the total memory in bytes.
#include <iostream>
using std::cout, std::endl;
int main() {
int values[] = {-5, 39, 13, -21}; // compiler determines number of elements
cout << "used memory: " << sizeof(values) << endl;
cout << "number of elements: " << sizeof(values) / sizeof(int) << endl;
return 0;
}
#include <cstdio>
void print_info(int values[], size_t count) { // values is actually a pointer
// sizeof returns the size of the pointer (64 bits on modern systems)
printf("sizeof(values): %ld\n", sizeof(values));
printf("sizeof(values) / sizeof(int): %ld\n", sizeof(values) / sizeof(int));
// only way to know the number of elements is a separate argument
printf("number of elements: %ld\n", count);
}
int main() {
int values[] = {-5, 39, 13, -21}; // compiler determines number of elements
printf("sizeof(values): %ld\n", sizeof(values));
printf("sizeof(values) / sizeof(int): %ld\n", sizeof(values) / sizeof(int));
print_info(values, sizeof(values) / sizeof(int));
return 0;
}
#include <cstdio>
int main() {
int first[] = {1, 2, 3};
int second[] = {4, 5, 6};
first = second; // compiler error: array type is not assignable
first = {7, 8, 9}; // also a compiler error
printf("{ %d, %d, %d }", first[0], first[1], first[2]);
return 0;
}
Download not_assignable.cpp
It is not possible to directly return an array from a function.
Its memory would be on the stack and the returned reference invalid.
There are three possible approaches
#include <cstdio>
#include <ctime>
#include <cstdlib>
void read_values(int values[], size_t count) {
srand(time(NULL)); // Initialize random number generator
for (size_t i = 0; i < count; ++i) {
values[i] = rand(); // assume the values are read from user input
}
}
int main() {
int values[3]; // number of elements known at compile time
read_values(values, 3);
printf("{ %d, %d, %d }\n", values[0], values[1], values[2]);
return 0;
}