+ 2

Why the size of an array is different in function??

#header void func(int array[] ){ size of array=4 } main() { int array=0,1,2,3,4,6 size of array=24 }

25th May 2021, 9:42 AM
Bot
Bot - avatar
6 Antworten
+ 7
When you pass an array to a function, array will decay. Decay means that array will be implicitly converted to a pointer that points to the first element of the array. So, in your function func() when you use size of operator you'll get the size of the pointer, which is usually 4 or 8 bytes (depending on the architecture of your machine).
25th May 2021, 10:13 AM
Lofty
Lofty - avatar
+ 2
Runtime Terror the link u have shared i didn't get the solution of my problem it covers only the use of sizeof operator(something like this) means they didn't cover the sizeof of, when an array passed to a function i found an article related to this on stackoverflow https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c#:~:text=To%20determine%20the%20size%20of,size_t%20n%20%3D%20sizeof(a)%3B&text=To%20determine%20the%20number%20of,size%20of%20the%20array%20element. Martin Taylor @Runtime Terror See the 2nd answer of that page(because it is related to my problem) and tell me about it please
26th May 2021, 2:01 AM
Bot
Bot - avatar
+ 1
Whats the full code? and using sizeof() returns the total size of whatever a = {0,1,2}; b = 7; usually: sizeof(b) will be 4 sizeof(a) will be 12
25th May 2021, 9:54 AM
Slick
Slick - avatar
+ 1
Programmer that's the confirmation of what said Martin Taylor (and firstly Quanti) was right against Runtime Terror, despite affirming the converse and claim for best answer ^^
26th May 2021, 2:12 AM
visph
visph - avatar
+ 1
Runtime Terror This may be a matter of multiple contexts resulting in an ambiguous interpretation of "_variable_" as used in your original explanation below: ------ "... sizeof the _variable_ at the first index of that array..." --- Specifically, "_variable_" may be interpreted to mean: a) the char type value or b) the pointer to the value It's not clear which meaning you were implying. Upon reviewing your most recent comment: ------ "... sizeof operator gives you the size of a _variable_ and/or datatype... ... even a pointer is a _variable_" --- I now believe your intended interpretation was (b). However, now, I'm not sure how to interpret this part of the phrase: "size of a variable _and/or_ datatype" ... as this may reveal some additional differences in respective understandings. I'm not trying to make the case for right or wrong. Rather, I'm attempting to explain the possible disconnect from unintended ambiguity.
27th May 2021, 8:41 AM
David Carroll
David Carroll - avatar
0
Size of an array is:- sizeof(arr)/sizeof(arr[0]) You can find it by arr.size() too.
27th May 2021, 2:00 AM
Vishal Pandey