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.
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:
Why don't I get a segmentation fault when I write beyond the end of an array?
(4 answers)
Closed 6 years ago.
I have declare an int array of just three elements but I notice that I can access to array indexes bigger
int x[3];
int length = sizeof(x)/sizeof(x[0]);
printf("\n the length defined is %i but I can still setting and getting other indexes")
One of the things that makes C fast is that it doesn't do any type of bounds checking on arrays. It expects programmers to know what they're doing to stay within the proper bounds.
Failure to do so means accessing a portion of memory outside the bounds of the array and leads to undefined behavior.
When accessing an index bigger than an array length, what you are actually doing is to "invade" a memory area that you are not supposed to access. This can lead to unexpected behavior of your application or a crash.
This question already has answers here:
C sizeof a passed array [duplicate]
(7 answers)
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 7 years ago.
I wrote the following function in C:
void dummy(int* my_array, int size)
{
//my implementation
}
Is there any way to check whether size is REALLY the size of my_array?
For example, if I call the function and use for my_array an array with 4 elements but pass 5 for size, is there any way to know that 5 is not really the size of the array?
Thank you in evidence.
You're looking at it the wrong way. An array is contiguous piece of memory. In C, you can represent this concept with a pointer to its start and its size. Since your array is represented by a <my_array, size> tuple, it doesn't make sense to talk about my_array's size, since it's only the start-pointer of the array.
unfortunately, there is no such way in C. As an array is always a pointer to an arbitrary address in memory, there is no such thing as "array bounds" that can be checked.
This is why many of the C functions have that special size parameter: because there's no other way of determining an array's size.
The size of the array is however big you made it when you declared and initialized it.
int my_array[100] = {0}; has a size of 100.
int my_other_array[50] = {0}; has a size of 50.
If you want the length of the data, then you're thinking in more abstract terms than the language can handle. The length of your data is a non-measurable parameter when the language does not support it.
If my_array is dynamically allocated (with malloc) you can get the size of memory block but it depends on specific compiler. Unfortunately in most cases there is address alignments and you will not have the exact size to 1 byte but aligned to 32 bits or other.
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.
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.