What are Built-in arrays |
What are Built-in arrays |
The "" operator defines arrays of char. Similarly, we can define an array of any
type with the [ ] operator :
int n[4] = { 1, 2, 3, 4 };
The compiler is able to determine by itself the size of an array, so you do not
have to specify it :
int poweroftwo[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
As we said, the compiler keeps the information about the size of arrays (or
strings), so the sizeof operator returns the size of the array as expected :
#include <iostream>
int main(int argc, char **argv) {
int hello[] = { 1, 2, 3 };
char something[] = "abcdef";
cout << sizeof(hello) << ’ ’ << sizeof(something) << ’\n’;
}
The size of arrays is always known and has to be known at compilation
time.
Note : from that, you can compute the number of element of an array by dividing
the sizeof the array by the sizeof the element type.