This question already has answers here:
Length of array in function argument
(9 answers)
how sizeof() works in pass by reference arguments
(6 answers)
Closed 8 years ago.
i just have a function " func(int input[])" . I want to find the length of array input and return the value.
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.
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.
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.
Related
This question already has answers here:
Returning an array using C
(8 answers)
Closed 2 years ago.
I want to refactor a function so that I can use it for arrays of different lengths and return that newly created array so that other functions can access it. I can't make the array static since you can't have static arrays with dynamic length. I also can't use a global struct because that needs to take the length of the array and that has to be hardcoded I think.
So the question is whether it is even possible to do something like this:
char* splitElementsArr(FILE* file){
int length = countBlankLines(file);
char *arr[length] // or maybe use malloc here
...Some operations to fill array
return arr;
No, you can't. You are returning a pointer to local data that were dynamically allocated on the stack. As soon as you return, those data no longer exist.
Solution : use malloc().
This question already has answers here:
Length of array in function argument
(9 answers)
Closed 5 years ago.
As far as I know, the following code can be used to obtain the length (meaning how many items it contains) of an array (in this example, an integer array):
int arrayOfInt[] = { 10, 20, 30, 40, 50 };
int lenght = sizeof(arrayOfInt) / sizeof(int);
This gives "5" as output, which is correct.
However, if i use the same code in a function, it does not work.
int getLenght(int intArray[])
{
return sizeof(intArray) / sizeof(int);
}
lenght = getLenght(arrayOfInt);
Using the function above, i get "1" as output. Which is not correct. Why does this happen? Am i doing something wrong or is it not possible?
This is not possible. Arrays, when passed as function argument, decays to the pointer to the first element, so essentially, inside the function,all you have is a pointer to the first element of the array.
Moreover, sizeof operator works on the supplied variable type, it has no way of knowing the actual type before the transformation.
To elaborate,
int getLenght(int intArray[])
and
int getLenght(int *intArray)
are functionally and behaviorally same. It's not an array anymore.
Having said that, just a suggestion, a more robust way of getting the number of elements in an array would be
int length = sizeof(arrayOfInt) / sizeof(arrayOfInt[0]);
This makes the expression independent of the hardcoded datatype, retaining the actual purpose.
This question already has answers here:
Is it possible to allocate array inside function and return it using reference?
(3 answers)
Closed 6 years ago.
I need to write a function that lets the user input the number of integers (n) in the array (A[n]) and the array itself, but I'm not sure how I should return both of them from the same function.
You could basically return a struct with one member as array of integers and another int giving number of elements in array .
This question already has answers here:
Length of array in function argument
(9 answers)
Closed 9 years ago.
How can I get arrayLength from user defined function?
int getArrayLength(int *arr){
//our logic
return result;
}
Simply: you cannot. You should pass it as another argument:
int getArrayLength(int *arr, int size){ ...
If you try with sizeof, it will return the size of the pointer. You can also use a special value to indicate the last element of your array (like 0 for strings), but adding a convention can make things more complicated.
You'll need to do one of two things:
Have the caller provide the length, or...
Agree on a sentinel value that lets you detect the end of the array.
In the general case, the right answer is option 1. You shouldn't write functions that take C arrays without also taking a length parameter.
In some specific cases, option 2 works pretty well. For example, \0 is used to mark the end of strings, which are just character arrays. If 0 isn't a valid value for the elements of array, that could work for cases other than strings. But generally, go with option 1.
Pass array length to the function too otherwise you can't. This is because sizeof(arr) will give you size of the pointer to int, not the size of entire array.
This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
C sizeof a passed array [duplicate]
(7 answers)
size of array in c
(6 answers)
Closed 9 years ago.
I am handling a function of type:
int xyz(int input[])
I do not have access to the main function and therefore have no idea about the size of the array.
How can i find the size of the input array?
Is there any way to know where the array ends?
sizeof(input)/sizeof(int*) is giving 1 as input is basically a pointer.
If the caller doesn't provide the size information about the array then there's no way to get the size in function xyz().
You can pass the size information in another variable (or as an elemnt of the array).
int xyx(int abc[], size_t len)
{
}
No... there's no (portable) way from within the called function, so it's normal in this situation (i.e. when no number-of-elements parameter is passed) for callers to adopt some convention such as providing a sentinel value in the last used element or guarantee a set number of elements. If a sentinel is used, you might be able to prove to yourself that this was being done by checking the calling code in a debugger.
If you pass an array to a function and don't provide the length, you can't find it, because the array decays to as pointer whose size is always 32 (64) bit.