flc.c 908 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // flc.c -- funny-looking constants
  2. #include <stdio.h>
  3. #define COLS 4
  4. int sum2d(const int ar[][COLS], int rows);
  5. int sum(const int ar[], int n);
  6. int main(void)
  7. {
  8. int total1, total2, total3;
  9. int * pt1;
  10. int (*pt2)[COLS];
  11. pt1 = (int [2]) {10, 20};
  12. pt2 = (int [2][COLS]) { {1,2,3,-9}, {4,5,6,-8} };
  13. total1 = sum(pt1, 2);
  14. total2 = sum2d(pt2, 2);
  15. total3 = sum((int []){4,4,4,5,5,5}, 6);
  16. printf("total1 = %d\n", total1);
  17. printf("total2 = %d\n", total2);
  18. printf("total3 = %d\n", total3);
  19. return 0;
  20. }
  21. int sum(const int ar[], int n)
  22. {
  23. int i;
  24. int total = 0;
  25. for( i = 0; i < n; i++)
  26. total += ar[i];
  27. return total;
  28. }
  29. int sum2d(const int ar[][COLS], int rows)
  30. {
  31. int r;
  32. int c;
  33. int tot = 0;
  34. for (r = 0; r < rows; r++)
  35. for (c = 0; c < COLS; c++)
  36. tot += ar[r][c];
  37. return tot;
  38. }