fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int fun(char *arr) {
  5. return sizeof(arr);
  6. }
  7.  
  8. int fun2(char arr[3]) {
  9. return sizeof(arr); // It's treating the array name as a pointer to the first element here too
  10. }
  11.  
  12. int fun3(char (&arr)[6]) {
  13. return sizeof(arr);
  14. }
  15.  
  16.  
  17. int main() {
  18.  
  19. char arr[] = {'a','b','c', 'd', 'e', 'f'};
  20.  
  21. cout << fun(arr); // Returns 4, it's giving you the size of the pointer
  22.  
  23. cout << endl << fun2(arr); // Returns 4, see comment
  24.  
  25. cout << endl << fun3(arr);
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
4
4
6