>To my knowledge I covered all of this
More or less. I believe the requirement was to allocate 10 spaces and fill them, then reallocate to 15 spaces without losing the first 10. You should also be more careful with return values and error checking when it comes to memory allocation:
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define Max 10
int main(void)
{
int i, NewMax;
char array[] = "abcdefghijklmnopqrstuvwxyz0123456789";
char *ptr_array;
/* Allocates 10 bytes of memory */
ptr_array = malloc(Max * sizeof *ptr_array);
/* Check that it worked */
if (ptr_array != NULL)
{
char *hold;
srand((unsigned)time(NULL));
/* Fill the 10 spots */
for (i=0; i<Max; i++)
ptr_array[i] = array[rand() % (sizeof(array)-1)];
/* Print all 10 characters */
for (i=0; i<Max; i++)
printf("%c", ptr_array[i]);
printf("\n");
/* Reallocated to 15, save in a second pointer in case of failure */
NewMax = Max + 5;
hold = realloc ( ptr_array, NewMax * sizeof *ptr_array );
/* Check that it worked */
if (hold != NULL)
{
/* Save the realloc'd memory to the original pointer */
ptr_array = hold;
/* Fill the next 5 spots */
for (i=Max; i<NewMax; i++)
ptr_array[i] = array[rand() % (sizeof(array)-1)];
/* Print all 15 characters */
for (i=0; i<NewMax; i++)
printf("%c", ptr_array[i]);
printf("\n");
}
free(ptr_array);
}
return 0;
}
-Prelude