0

I am trying to create a static array where the length of the array could differ. I am doing this so that i can return the array from a function without using pointers.

I have tried:

void arrExample1(int size){
    #define len size
    satic int arr[len];
}
void arrExample2(int size){
    enum { len = size };
    static int arr[len];
}

Neither of these have worked and I am at a loss of what I should do next. Can I get some advice or pointed in the correct direction?

3
  • 1
    "I am doing this so that i can return the array from a function without using pointers.": That's not typically a good idea. Allocate memory with malloc instead (and free it later) or let the caller pass a pointer to an array of the correct size to the function. The latter approach allows the caller to choose where the memory resides. Commented Mar 17, 2023 at 23:02
  • That's just the thing: the length of an array with static storage duration cannot differ. The lifetime of such an array is the entire run of the program, so not only can it not change, but the size must be known at compile time. Commented Mar 17, 2023 at 23:07
  • “Static” means, just as a plain English word, “lacking in movement, action, or change.” You should not expect static objects to vary. Commented Mar 17, 2023 at 23:12

1 Answer 1

0

Unfortunately you can only define it dynamicly. Here is how, in case u need it: Dynamicly creating an array

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.