Arrays

An array is a list of similar data, stored in contiguous memory locations. For example, a list of names of people in a class can be stored as a string array.

Note:

Arrays should be used when you know in advance (while you're writing your code) how many elements will be stored in the array. For example, if you know that the number of students in your class is 67, and this number would never change, you can use an array of size 67 to store the names or ID's of everyone in your class.

But if the number of students can change, then it's better to use pointers instead.

Note that:

#include void main( ) { int MyArray[100]; MyArray[0] = 101; MyArray[99] = 202; printf("MyArray[0] = %d\nMyArray[99] = %d\n", MyArray[0], MyArray[99]); }

Here's the output:

UNIX prompt >> a.out
MyArray[0] = 101
MyArray[99] = 202

 

Here's what the MyArray looks like in memory:

 

 

Important
If you declare an MyArray of size 100, it's range is 0 to 99. That is, the first element of the array is element with index 0, and the last element is the one with index 99. There's no element with index 100 in the array.

Also note that there's no way to print the entire array as a whole. It has to be printed element-by-element.


More Examples

Back to Index