fork download
  1. #include <stdio.h>
  2.  
  3. #define ROWS 2
  4. #define COLS 3
  5.  
  6. void copy_ptr(double *src, double *dest, int len);
  7. void copy_ptr2d(int rows, int cols, double (*src)[cols], double (*dest)[cols]);
  8.  
  9. int main(void) {
  10. double array[ROWS][COLS] = { { 12.3, 55.1 }, { 33.6, 21.9, 90.8 } };
  11. double array2[ROWS][COLS];
  12. printf("Array { { 12.3, 55.1 }, { 33.6, 21.9, 90.8 } }\n");
  13. printf("Array copy:\n");
  14. copy_ptr2d(ROWS, COLS, array, array2);
  15. for (int i = 0; i < ROWS; i++) {
  16. for (int j = 0; j < COLS; j++) {
  17. printf("array2[%d][%d]: %.1f\n", i, j, array2[i][j]);
  18. }
  19. printf("\n");
  20. }
  21. return 0;
  22. }
  23.  
  24. void copy_ptr2d(int rows, int cols, double (*src)[cols], double (*dest)[cols]){
  25. for (int i = 0; i < rows; i++) {
  26. copy_ptr(*src++, *dest++, cols);
  27. }
  28. }
  29.  
  30. void copy_ptr(double *src, double *dest, int len) {
  31. for (int i = 0; i < len; i++) {
  32. *dest++ = *src++;
  33. }
  34. }
Success #stdin #stdout 0s 9416KB
stdin
Standard input is empty
stdout
Array { { 12.3, 55.1 }, { 33.6, 21.9, 90.8 } }
Array copy:
array2[0][0]: 12.3
array2[0][1]: 55.1
array2[0][2]: 0.0

array2[1][0]: 33.6
array2[1][1]: 21.9
array2[1][2]: 90.8