Your top array (the one containing arrays) should be defined something like (assuming you are storing
ints):
int[] lists[ 10 ]; // Ten arrays of variable length.
Now you must decide how to know how long each array is. You could store its length as the first integer in the array. For example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int[] lists[ 10 ];
int i, j;
int *make_int_subarray( int length );
// Initialize
for (i = 0; i < 10; i++)
lists[i] = NULL;
// Fill some of the sub-arrays
for (i = 0; i < 6; i++)
lists[i] = make_int_subarray( (i+1) * 2 );
// Print the arrays
puts( "The arrays are:" );
for (i = 0; i < 10; i++)
{
for (j = 1; j < lists[i][0]; j++)
printf( "%i ", &lists[i][j] );
printf( "\n" );
}
// Free all memory
for (i = 0; i < 10; i++)
if (lists[i] != NULL)
{
free( lists[i] );
lists[i] = NULL;
}
return 0;
}
int *make_int_subarray( int length )
{
int i;
// Allocate storage for the array
int *result = (int *)malloc( (length+1) * sizeof(int) );
// Store its length
result[0] = length;
// Fill it with numbers: 1, 2, 3, ...
for (i = 1; i <= length; i++)
result[i] = i;
return result;
}
Hope this helps.
Last edited by Duoas : May 16th, 2008 at 10:27 pm. Reason: Made it compilable.