Advertisement
KDOXG

N-Dimensional Array

Jun 27th, 2019
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. typedef int data;
  2.  
  3. struct ArrayN_Dimension{
  4.     struct ArrayN_Dimension *nextDim;
  5.     data *array;
  6. };
  7.  
  8. data* getDimension(struct ArrayN_Dimension *head, int dim)
  9. {
  10.     if (dim < 0)
  11.         return NULL;
  12.     if (dim == 0)
  13.         return head->array;
  14.     else if (head->nextDim == NULL)
  15.             return NULL;
  16.         else
  17.             return getDimension(head->nextDim,dim-1);
  18. }
  19.  
  20. void createarray(struct ArrayN_Dimension *head, int n)
  21. {
  22.     if (n < 0)
  23.         return;
  24.     if (n == 0)
  25.         return;
  26.     head = malloc(sizeof(struct ArrayN_Dimension));
  27.     head->nextDim = NULL;
  28.     head->array = malloc(n*sizeof(data));
  29.     createarray(head->nextDim,n-1);
  30.     return;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement