- #1
yungman
- 5,726
- 242
I have a strange problem. I pass an Array[20] to a function. When I put cout << sizeof(Ar); (line 29 in the program) it only shows the length of one element ( 4bytes). BUT if I write cout << Ar[10]; (line 30 in the program) It will give me the correct number of the original idNum[10]. I tried a few and it works. Obviously the idNum has successfully passed to the function. But I cannot get the size.
I even put a multiply by 2 in the function so all the values are doubled before return. You can see in line 19 that I display the content of idNum and show that the value doubled. So the passing the parameters are working. I checked a lot of times, I can't figure this out. Please advice.
Also, when I pass an array to function, I have to at least put another int number or VS will flag an error. Why? eg binarySearch(idNums, Hid);
Thanks
C++:
#include <iostream>
using namespace std;
void binarySearch(int[], int);//Function
const int size = 20;
int main()
{
//Array with employee IDs sorted in ascending order.
int idNums[] = { 101, 142, 147, 189, 199, 207, 222,
234, 289, 296, 310, 319, 388, 394,
417, 429, 447, 521, 536, 600 };
int Hid; // to hold search result
Hid = { sizeof(idNums) / sizeof(idNums[0]) };
cout << " Size of idNums = " << Hid << "\n\n";
binarySearch(idNums, Hid);
cout << " Size of idNums after calling the function = " << sizeof(idNums) << "\n\n";
for (int i = 0; i < 20; i++)
cout << idNums[i] << " ";
cout << "\n\n";
return 0;
}
void binarySearch(int Ar[], int Last)
{
Last = 0;
cout << " Size of Ar = " << sizeof(Ar) << "\n\n";
cout << " Ar[10] = " << Ar[10] << "\n\n";
for (int index = 0; index < 20; index++)
Ar[index] *= 2; // double the value.
}
I even put a multiply by 2 in the function so all the values are doubled before return. You can see in line 19 that I display the content of idNum and show that the value doubled. So the passing the parameters are working. I checked a lot of times, I can't figure this out. Please advice.
Also, when I pass an array to function, I have to at least put another int number or VS will flag an error. Why? eg binarySearch(idNums, Hid);
Thanks
Last edited: