How to define an array inside a function in C?

Posted by Arunav Dev on Stack Overflow See other posts from Stack Overflow or by Arunav Dev
Published on 2012-10-02T21:32:01Z Indexed on 2012/10/02 21:37 UTC
Read the original article Hit count: 130

Filed under:
|

So in my source file I have the folowin function:

void update(state* old_state, state* measurement, uint32_t size)
{
  state new_state[size];
  //some function using measurement and old_state and returning the result in newstate

  arm_fadd_32(measurement,old_state,newstate,size);

// rest of the code }

Now the compiler throws an error saying that error#28:expression must have a constant value. I think it's due to the fact that even though inside the method the size local variable is not changing the compiler is expecting a constant while defining the size. I have tried the following:

int const a = size; 

and then tried to reinitialize it says constant value is not known. I did some research in internet and it appears that there is no easier way without using malloc, which I don't want to since I am using the code for some embedded application.

Is there a way to avoid this problem without really using malloc? Thanks in advance guys!

© Stack Overflow or respective owner

Related posts about c

    Related posts about arrays