I am required to pass a constant-size array to a function in C, but only use part of that array. Specifically, the following pseudo-code explains my situation:
void my_function(int arr_end,double arr[20]) {
Create new array arr_new that contains elements 0 to arr_end or arr
Use arr_new
}
So far, I tried to do the following:
void my_function(int arr_end,double arr[20]) {
double arr_new[arr_end+1];
int i;
for(i=0;i<=arr_end;i++){
arr_new[i] = arr[i];
}
// Now I can do what I want with arr_new
}
But I get the error: int arr_end expression must have a constant value
. Here's a screenshot of the error in Visual Studio Community 2015:
My problem is that arr_end
is not constant (it means that at different times I want to extract a different portion of arr
).
How can I achieve what I want (make arr_new
contain a part of arr
) using only for basic code and without malloc
or anything like this? Thanks!