RSS Forums RSS
Please support our C advertiser: Programming Forums

Row length changes?

Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,884
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 197
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Row length changes?

  #7  
May 16th, 2008
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:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. int[] lists[ 10 ];
  7. int i, j;
  8.  
  9. int *make_int_subarray( int length );
  10.  
  11. // Initialize
  12. for (i = 0; i < 10; i++)
  13. lists[i] = NULL;
  14.  
  15. // Fill some of the sub-arrays
  16. for (i = 0; i < 6; i++)
  17. lists[i] = make_int_subarray( (i+1) * 2 );
  18.  
  19. // Print the arrays
  20. puts( "The arrays are:" );
  21. for (i = 0; i < 10; i++)
  22. {
  23. for (j = 1; j < lists[i][0]; j++)
  24. printf( "%i ", &lists[i][j] );
  25. printf( "\n" );
  26. }
  27.  
  28. // Free all memory
  29. for (i = 0; i < 10; i++)
  30. if (lists[i] != NULL)
  31. {
  32. free( lists[i] );
  33. lists[i] = NULL;
  34. }
  35.  
  36. return 0;
  37. }
  38.  
  39. int *make_int_subarray( int length )
  40. {
  41. int i;
  42. // Allocate storage for the array
  43. int *result = (int *)malloc( (length+1) * sizeof(int) );
  44.  
  45. // Store its length
  46. result[0] = length;
  47.  
  48. // Fill it with numbers: 1, 2, 3, ...
  49. for (i = 1; i <= length; i++)
  50. result[i] = i;
  51.  
  52. return result;
  53. }

Hope this helps.
Last edited by Duoas : May 16th, 2008 at 10:27 pm. Reason: Made it compilable.
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:11 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC