Add new term to array

Quick question: say I wanted to add a new term to an array every time condition A is true, and I wanted exactly how many terms I stored to be the length of the array. example:

int count = 1;
int array[] = {0};

void loop(){
if(A == true){
array[count] = valueFromSensorOrWhatever;
count = count + 1;
}
}

if(B == true){
for(i = 0; i < count, i++){
Serial.println(array[count]);
}

I think I've read that the compiler 'eliminates' arrays? I know that I could just define array[] to have a hundred terms, but then I'd have a bunch of zeroes at the end wasting space. Any ideas?

I just realized I wrote that for loop wrong, but the intent was to just print every stored value to the serial port ;D

You will encounter problems with your code. The variable array will only be 2 bytes (int), and since the array variable will not increase its size automatically you will be writing to other parts of the memory. Make sure you initialize the correct size first. How much do you need? 100 ints? THen

int array[100];

will work (and is needed).

Please look up - in any of 1234 C-articles in the internet - how C-arrays work.
And please don't write things as
if (A==true) ...
which is not in the spirit of C (and in neither language's spirit I am aware of.)

Well, I would just give it the amount I need, but I don't know how much it will need. It might need two values; it might need twenty. I want it to be able to adapt to whatever happens.

the A == true bit was just an example, I would definitely not put that into a real code. It would be something like reading a sensor and then if that value is noteworthy, write to the array.

What you are proposing to do is called "dynamic memory allocation", dynamically create a spot in memory for a new array variable as they come. It's possible, sort of (using malloc trickery), but on a resource-constrained system (like a '328 with 2k of RAM) the overhead of maintaining dynamically-allocated memory is high and probably not worth it.

It would be best to estimate the maximum number of elements you need ahead of time and declare an array as previously suggested. Just take note that you've only got 2k of RAM (assuming an ATmega328), an 'int' is 2 bytes, so you should stay well away from 1k elements in the array.

--
The Quick Shield: breakout all 28 pins to quick-connect terminals

Hello,
What the others have said is perfectly true. Dynamic arrays in c are very difficult to implement (for a novice). Have you considered using the eeprom memory to store these values? You could even research data structures in c and c++ and design your own vector<> class. Take a look at the c++ STL for some idea's.

okay, great. I don't really have a problem with allocating more than enough space, but I figured it would be better coding practice to only use what you need. I guess if there isn't a really easy way to do it then I'll go ahead and just give it 100 or so. Thanks everyone!

I figured it would be better coding practice to only use what you need.

This is only true if something else needs the memory that you aren't using. If nothing else can use the memory, there is no advantage to not dedicating the max amount to a single function.