-
Notifications
You must be signed in to change notification settings - Fork 2
Array
int foo[5];
These are arrays whose number of dimensions and their size are known at compile time. Array bucket values are stored in contiguous memory locations (thus pointer arithmetic can be used to iterate over the bucket values), and 2D arrays are allocated in row-major order.
Statically declared arrays can be declared as either global or local variables.
int *p_array = (int *)malloc(sizeof(int)*50);
Dynamically allocated arrays are allocated on the heap at run time. The heap space can be assigned to global or local pointer variables that store the address of the allocated heap space (point to the first bucket).
To dynamically allocate space, use calls to malloc passing in the total number of bytes to allocate
(always use the sizeof to get the size of a specific type).
A single call to malloc allocates a contiguous chunk of heap space of the passed size.
int foo[5] = {};int foo[5] = {1, 2, 3, 4, 5};int foo[5] {1, 2, 3, 4, 5};
for (int i = 0; i < LEN; i++)
cin >> keywords[i];
-
string keywords[NUM];:stringarray -
#define len(arr) sizeof(arr) / sizeof(*arr): length of array