PDA

View Full Version : Array length



logical
03-04-2012, 10:49 PM
Can someone please explain this?
The length function does not return what the same code does in the main function

#include <iostream>

using namespace std;

int arrLength(int arr[]);

int main(){
int arr[] = {1, 2, 3, 4, 25, 42};
int l;

cout << "inside main: "<< sizeof(arr) << "/" << sizeof(arr[0]) << "\n";

l = sizeof(arr) / sizeof(arr[0]);
cout << "sizeof L:" << l << endl;

l = arrLength(arr);
cout << "length L:" << l << endl;

}

int arrLength(int arr[]){
cout << "inside function: " << sizeof(arr) << "/" << sizeof(arr[0]) << "\n";
return sizeof(arr) / sizeof(arr[0]);
}


Why does this output:

inside main: 24/4
sizeof L:6
inside function: 4 4
length L:1

Process returned 0 (0x0) execution time : 1.833 s
Press any key to continue.


PS - the only thing i seem to be posting of these forums are questions >.< hope that's ok.

x[Warrior]x3500
03-04-2012, 10:55 PM
http://www.cplusplus.com/forum/general/28539/

ur just gettin the byte size of everything using the sizeOf();