Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

How to assign values to the whole row of a dynamic two dimensional array

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 2.18 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. searches->library[0][0] = {2, 3, 4, -1};
  2. searches->library[1][0] = {4, 5, 6, -1};
  3. searches->library[2][0] = {2, 3, 4, 5, 6, -1};
  4. searches->library[3][0] = {0, 1, 2, 3, 4, 5, 6, 7, -1};
  5. searches->library[4][0] = {0, 1, 2, -1};
  6. searches->library[5][0] = {0, 6, 7, -1};
  7. searches->library[6][0] = {0, 1, 2, 6, 7, -1};
  8. searches->library[7][0] = {0, 1, 2, 3, 4, -1};
  9. searches->library[8][0] = {0, 4, 5, 6, 7, -1};
  10.        
  11. searches->library[][9] = {{2, 3, 4, -1},
  12.                                                  {4, 5, 6, -1},
  13.                                                  {2, 3, 4, 5, 6, -1},
  14.                                                  {0, 1, 2, 3, 4, 5, 6, 7, -1},
  15.                                                  {0, 1, 2, -1},
  16.                                                  {0, 6, 7, -1},
  17.                                                  {0, 1, 2, 6, 7, -1},
  18.                                                  {0, 1, 2, 3, 4, -1},
  19.                                                  {0, 4, 5, 6, 7, -1}};
  20.        
  21. typedef struct{
  22.     int active_length;  // Size of active array of searches
  23.     int* active;                // Active array of searches
  24.     int** library;      // Library of array of searches
  25. } SearchLibrary;
  26.        
  27. SearchLibrary* searches;
  28. searches = (SearchLibrary *) malloc(sizeof(SearchLibrary*));
  29. int search_cases = 9, search_directions = 9;
  30. searches->library = (int **) malloc(search_cases * sizeof(int *));
  31. searches->active = (int *) malloc(search_directions * sizeof(int));
  32.  
  33. int i;
  34. for(i = 0; i < search_cases; i++){
  35.     searches->library[i] = (int *) malloc(search_directions * sizeof(int));
  36. }
  37.        
  38. static const int Library0[] = {2, 3, 4, -1};
  39. static const int Library1[] = {4, 5, 6, -1};
  40. static const int Library2[] = {2, 3, 4, 5, 6, -1};
  41. static const int Library3[] = {0, 1, 2, 3, 4, 5, 6, 7, -1};
  42. static const int Library4[] = {0, 1, 2, -1};
  43. static const int Library5[] = {0, 6, 7, -1};
  44. static const int Library6[] = {0, 1, 2, 6, 7, -1};
  45. static const int Library7[] = {0, 1, 2, 3, 4, -1};
  46. static const int Library8[] = {0, 4, 5, 6, 7, -1};
  47.  
  48. static const int * Library[] = {
  49.     Library0, Library1, Library2,
  50.     Library3, Library4, Library5,
  51.     Library6, Library7, Library8,
  52. };
  53.  
  54. typedef struct{
  55.     int active_length;  // Size of active array of searches
  56.     const int* active;                // Active array of searches
  57.     const int** library;      // Library of array of searches
  58. } SearchLibrary;
  59.  
  60. searches->library = Library;
  61.        
  62. #define SEARCH_DIRECTIONS 9
  63.  
  64. memcpy(searches->library[k], ((int [SEARCH_DIRECTIONS]){ 1, 2, 3 }),
  65.     sizeof(int) * SEARCH_DIRECTIONS);