Subscripts are used when accessing arrays. An array is a contiguous block of memory containing two or more elements of the same type. Since each element is the same length, it is trivial to work out the offset address of one element relative to another using a zero-based index. For any type T, the type T[n] is an array of Ts with indices in the range 0 to n-1. We can also create arrays at runtime by allocating sufficient memory on the heap for the given type:
void f(int n) {
if (n<1) return; // cannot allocate 0 elements!
int* ptr = malloc (n * sizeof(int)); // allocate memory for n integers
if (ptr!=0) return; // ensure memory was allocated!
ptr[0] = 42; // use subscript operator to assign a value to the first element
// assign to other elements...
// use array...
free (ptr); // release memory as soon as we're finished with it
}
Copyright © 2026 eLLeNow.com All Rights Reserved.