53 lines
876 B
C
53 lines
876 B
C
// Implements a list of numbers with an array of dynamic size
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int main(void)
|
|
{
|
|
// List of size 3
|
|
int *list = malloc(3 * sizeof(int));
|
|
if (list == NULL)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
// Initialize list of size 3 with numbers
|
|
list[0] = 1;
|
|
list[1] = 2;
|
|
list[2] = 3;
|
|
|
|
// List of size 4
|
|
int *tmp = malloc(4 * sizeof(int));
|
|
if (tmp == NULL)
|
|
{
|
|
free(list);
|
|
return 1;
|
|
}
|
|
|
|
// Copy list of size 3 into list of size 4
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
tmp[i] = list[i];
|
|
}
|
|
|
|
// Add number to list of size 4
|
|
tmp[3] = 4;
|
|
|
|
// Free list of size 3
|
|
free(list);
|
|
|
|
// Remember list of size 4
|
|
list = tmp;
|
|
|
|
// Print list
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
printf("%i\n", list[i]);
|
|
}
|
|
|
|
// Free list
|
|
free(list);
|
|
return 0;
|
|
}
|