About Arrays


I was wondering how i added a number to an empty array. Like if i made an: int array1[] = {} then later wanted to make a number appear in the array like eg. 1 I am writing a program that adds numbers to an array and then checks the array afterwards with a correct array, if the two arrays are both the same, then it runs another part of the program.. Is this possible ? and if yes, then how ?

regards Lungefisk

When you combine the initialization of the array with the declaration of an array, like this:

int array1[] = {};

You are telling the compiler to figure out how many elements the array can hold. In this case, it's 0 because the initialization data is missing.

You will never be able to add an element to this array.

You can create an empty array, of a fixed size:

int array1[10];

This array can hold 10 elements. You can put items in the array anywhere, as long as the item is an int.

If you are going to compare what's in the array at some point to another "correct" array, you must know the size of the array.

You need to add error handling when (trying to) add(ing) items to the array, to make sure that the index into the array is a valid index.