0

(c++) what is the difference between passing an array to a function then finding its length and directly finding its length?

Each is giving a different output.. How to fix this problem? https://code.sololearn.com/c7EmmsCBKYL3/?ref=app

15th Jan 2022, 3:21 PM
ABHISHEK
ABHISHEK - avatar
5 Réponses
+ 1
Abhishek Mazumder It's because arrays are treated as pointers in C whereas they actually are not. So when you pass array as an argument where the parameter is a pointer, what you do is sizeof( int * ) / sizeof ( int ) which makes the answer same no matter what the size of the actual array is. In my system it outputs 2 all the time as a pointer to an integer is 8 bytes and an integer is 4 bytes.
15th Jan 2022, 4:38 PM
Mustafa K.
Mustafa K. - avatar
0
but each is giving a different output
15th Jan 2022, 4:29 PM
ABHISHEK
ABHISHEK - avatar
0
ok got it Thanks
15th Jan 2022, 4:45 PM
ABHISHEK
ABHISHEK - avatar
0
but come we find the size of an array inside a function
15th Jan 2022, 4:45 PM
ABHISHEK
ABHISHEK - avatar
0
Mustafa K. Thank You very much I found it..
15th Jan 2022, 4:47 PM
ABHISHEK
ABHISHEK - avatar