Hi! I'm a new C programmer, and I just took my final exam! It was complicated, but I understood everything except how to store the results of a specific function's returns into dynamic memory.

The test was a lot longer and more complicated than the code I'm going to post, but basically the user was in a menu ( do / while loop ) that would have them enter a number, send that number to a function. The function would then break up the number and do all sorts of nefarious things to it, then return the mutilated int it crunched out.

The program was supposed to keep track of the returned values into dynamic memory, and then print them out in the end. I understand how to grab a chunk of dynamic memory using malloc, but I don't understand if I have a certain chunk of dynamic memory, how to add onto it, and keep the previous values in the memory intact.

Here is the sample code I wrote that kind of mimics my final ( very basic )

Code:
/**
*Program Name:      junk test
*Written  By:       [email protected]
*Discussion:        using pointers
*Compiler:          gcc
*/

#include <stdio.h>
#include <stdlib.h>

int square( int );
        /* returns the square of a number */

int main( void ) {

        int number, theSquare, count = 0; // ints to hold stuff
        int* pointer;  // my pointer
        int i;  // index var for "for" loops

        do {

        printf( "\nHello, lover, what number would you like to square?"
                "\n  Enter a negative number to quit : " );
        scanf( "%d", &number );

        if ( number >= 0 ) {
                count++;
                theSquare = square( number );
                printf( "\nThe square of %d is %d\n", number, theSquare );
                /* I'm sure this is where Im getting my code wrong.  I want to have a way to add
                 * onto dynamic memory 4 bytes at a time ( int's size ) and then add the newly found
                 * square that was returned into that spot
                 */
                pointer = ( int* ) malloc( sizeof( int ) * count );
                *( pointer + count ) = theSquare; 
        }

        } while ( number >= 0 );

        printf( "\nHere were the squares that I found:\n" );

        for ( i = 0; i < count; i++ ) {
                printf( "\n%d", *( pointer + count ) );

        }

        free( pointer );
  
return 0;
}

int square( int arg ) {

return arg * arg;
}

/* OUTPUT:
Hello, lover, what number would you like to square?
  Enter a negitive number to quit : 3

The square of 3 is 9

Hello, lover, what number would you like to square?
  Enter a negitive number to quit : 4

The square of 4 is 16

Hello, lover, what number would you like to square?
  Enter a negitive number to quit : 5

The square of 5 is 25

Hello, lover, what number would you like to square?
  Enter a negitive number to quit : 6

The square of 6 is 36

Hello, lover, what number would you like to square?
  Enter a negitive number to quit : 7

The square of 7 is 49

Hello, lover, what number would you like to square?
  Enter a negitive number to quit : -1

Here were the squares that I found:

49
49
49
49
49
*/
I'm sure there has to be a way to add onto memory already allocated, but our teacher never told us how to do it...maybe it's simple and I just don't get the logic? Thanks for the help!!