fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include<stdio.h>
  5. void fun(int arr[])
  6. {
  7. int i;
  8.  
  9. /* sizeof should not be used here to get number
  10.   of elements in array*/
  11. int arr_size = sizeof(arr)/sizeof(arr[0]); /* incorrect use of sizeof*/
  12.  
  13. for (i = 0; i < arr_size; i++)
  14. {
  15. arr[i] = i; /*executed only once */
  16. }
  17. }
  18.  
  19. int main()
  20. {
  21. int i;
  22. int arr[4] = {0, 0 ,0, 0};
  23. fun(arr);
  24. std::cout<<sizeof(arr[0]);
  25. /* use of sizeof is fine here*/
  26. for(i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
  27. printf(" %d " ,arr[i]);
  28.  
  29. getchar();
  30. return 0;
  31. }
Success #stdin #stdout 0s 4512KB
stdin
Standard input is empty
stdout
4 0  1  0  0