I am new to C/C++ and am learning about arrays. I just finished reading about passing arrays to functions, and am trying to implement it. I start off with an array x[] = "Hello
, which of course has a sizeof()
6. But for some reason, when I pass it to my function, listElements(char arr[])
, then I get sizeof(arr)
equal to 4. Why is my array being cut off like this?
Also, sizeof(char)
on my compiler is 1.
#include <iostream>
using namespace std;
void listElements(char[]);
int main(){
char x[] = "Hello";
cout << sizeof(x) << endl; // 6
listElements(x);
cin >> x[0];
return 0;
}
void listElements(char arr[]){
cout << "Size of arr: " << sizeof(arr) << endl; // 4 <--- why is this 4?
cout << "Size of char: " << sizeof(char) << endl; // 1
for (int j = 0; j < (sizeof(arr) / sizeof(char)); j++){
cout << arr[j] << endl;
}
return;
}