fork(2) download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <inttypes.h>
  4.  
  5. int main()
  6. {
  7. int n = 2;
  8. int m = 3;
  9. // int i[2][3] = { {1, 2, 3}, {4, 5, 6} };
  10. int * i = malloc(sizeof(int) * n * m); // i[n][m]
  11. for(int j = 0; j < n * m; ++j) i[j] = j + 1;
  12. for(int j = 0; j < n; ++j)
  13. {
  14. for(int k = 0; k < m; ++k)
  15. printf("%d ", *(i + j * m + k));
  16. printf("\n");
  17. }
  18.  
  19. int (*ii)[m] = (int(*)[m]) i;
  20. for(int j = 0; j < n; ++j)
  21. {
  22. for(int k = 0; k < m; ++k)
  23. printf("%d ", ii[j][k]);
  24. printf("\n");
  25. }
  26. ii = (int(*)[m]) i;
  27. printf("i: %" PRIuPTR "\n", i);
  28. printf("ii: %" PRIuPTR "\n", ii);
  29. printf("sizeof(i): %" PRIuPTR "\n", sizeof(i));
  30. printf("sizeof(ii): %" PRIuPTR "\n", sizeof(ii));
  31. printf("sizeof(*ii): %" PRIuPTR "\n", sizeof(*ii));
  32. free(i);
  33. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
1 2 3 
4 5 6 
1 2 3 
4 5 6 
i: 94004084805648
ii: 94004084805648
sizeof(i): 8
sizeof(ii): 8
sizeof(*ii): 12