i just have a function " func(int input[])" . I want to find the length of array input and return the value.
Asked
Active
Viewed 125 times
3 Answers
2
You will not be able to do that in a function. The length of the array will need to be passed as additional argument to the function. An array passed to a function decays. See this thread.

Community
- 1
- 1

Ivaylo Strandjev
- 69,226
- 18
- 123
- 176
1
You cannot. In func
, that input
will degrade to pointer to int
.
However, after define that input
array, you could find out how many elements in it by
int input[] = {1, 2, 3, 4};
size_t num_of_element = sizeof(input)/sizeof(input[0]);
And if you need to pass that input
to a function that needs to know how many elements there are in that array, you need to pass that length to it as an argument.

Lee Duhem
- 14,695
- 3
- 29
- 47
0
Usually not possible in c. This is also the reason why it is possible to write outside an array in this language. You always have to pass the length as parameter.

nils
- 1,362
- 1
- 8
- 15