Advertisement
M0_0Y

[C]: How I think 3D arrays expressed in C.

Dec 2nd, 2022
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. /*
  2.     int a[3];
  3.    
  4.     [1, 2, 3]
  5.      |  |  |
  6.      /  |  \
  7.     50  60  70
  8.     ----------
  9.    
  10.     int a[3][2]
  11.    
  12.     1, 2
  13.     3, 4
  14.     5, 6
  15.     ----------
  16.    
  17.     int a[5];
  18.    
  19.     1, 2, 3, 4, 5 -- 1 dimensional
  20.    
  21.     int a[5][4];
  22.    
  23.     1, 2, 3, 4 }
  24.     5, 6, 7, 8
  25.     9, 10, 11, 12 } -- 2 dimensional
  26.     13, 14, 15, 16
  27.     17, 18, 19, 20 }
  28.    
  29.     ----------
  30.     int a[5][4][2]
  31.     3D = {
  32.             1,  3,  5,  7,
  33.                 *[1: 1, 2], *[3: 3, 4], *[5: 5, 6], *[7: 7, 8]
  34.             9,  11, 13, 15,
  35.                 *[9: 9, 10], *[11: 11, 12], *[13: 13, 14], *[15: 15, 16]
  36.             17, 19, 21, 23,
  37.                 *[17: 17, 18], *[19: 19, 20], *[21: 21, 22], *[23: 23, 24]
  38.             25, 27, 29, 31,
  39.                 *[25: 25, 26], *[27: 27, 28], *[29: 29, 30], *[31: 31, 32]
  40.             33, 35, 37, 39,
  41.                 *[33: 33, 34], *[35: 35, 36], *[37: 37, 38], *[39: 39, 40]
  42.     }
  43.        
  44.     (size = 40) (for 3D)
  45. */
  46.  
  47. #include <stdio.h>
  48.  
  49. int main()
  50. {
  51.     int intsize = sizeof(int);
  52.    
  53.     int a_1d[5];
  54.     int a_2d[5][4];
  55.     int a_3d[5][4][2];
  56.    
  57.     int size_a1d = sizeof(a_1d) / intsize;
  58.     int size_a2d = sizeof(a_2d) / intsize;
  59.     int size_a3d = sizeof(a_3d) / intsize;
  60.    
  61.     printf("(Dimensional sizes with sets: {5, 4, 2})\n\n1D[%d]: %lu,\n2D[%d][%d]: %lu,\n3D[%d][%d][%d]: %lu;", 5, size_a1d, 5, 4, size_a2d, 5, 4, 2, size_a3d); // matches predictions
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement