We have been given a large array of unknown size with elements given , is there any function or something other through which we can find the size of that array in language C int a[]={4,6,4,26,3,2,5,7,3,7,3,2,5,4,6,3,7,232,6,32,6,3,7,3,6,2,5,7,3,6,3,6,36,3,67,23,6};
Asked
Active
Viewed 1,758 times
-2
-
1Given how? In an envelope? – Eugene Sh. Aug 02 '18 at 18:48
-
int a[]={4,6,4,26,3,2,5,7,3,7,3,2,5,4,6,3,7,232,6,32,6,3,7,3,6,2,5,7,3,6,3,6,36,3,67,23,6}; – Azam Aug 02 '18 at 18:50
-
The sizeof() function is your friend. – Uclydde Aug 02 '18 at 18:51
-
1This size is known. – Eugene Sh. Aug 02 '18 at 18:53
-
#include
It does not help either int main() {int a[]={4,6,4,26,3,2,5,7,3,7,3,2,5,4,6,3,7,232,6,32,6,3,7,3,6,2,5,7,3,6,3,6,36,3,67,23,6}; printf("%d",sizeof(a)); } – Azam Aug 02 '18 at 18:55 -
In this way you get the size in bytes, to print the number of elements use `printf("%zu\n", sizeof(a) / sizeof(*a));` – David Ranieri Aug 02 '18 at 18:56
-
1@Azam It is known, it just isn't readily apparent. By contrast, if you were to pass that array in as an argument to a function, then as far as that function was concerned, the size of the array would be unknown as it would decay to a pointer and thus `sizeof` would give you the size of the pointer and not the number of bytes in the array. – Christian Gibbons Aug 02 '18 at 18:58
-
Thanks everyone , problem solved – Azam Aug 02 '18 at 18:59
-
if you dont have an array, but just a degenerate array, which doesn't have a length you will have to null terminate it and iterate it – Grady Player Aug 02 '18 at 19:23
-
@GradyPlayer Got it,Thanks – Azam Aug 02 '18 at 19:32
-
Upvoting to offset some of the unexplained, and thus unhelpful, downvotes. – Richard Aug 02 '18 at 20:10
1 Answers
1
The size in bytes you can get by
sizeof(a);
The number of elements in that array you can get by
sizeof(a)/sizeof(a[0]);

Yunnosch
- 26,130
- 9
- 42
- 54