1

When I define an array like

double *first = (double *)malloc( N*N*sizeof( double ) );

There is not problem. But when I specify

static double *first = (double *)malloc( N*N*sizeof( double ) );

I get this error

error: initializer element is not constant
   10 |     static double *first = (double *)malloc( N*N*sizeof( double ) );
      |                            ^

How can I fix that?

1

1 Answer 1

3

You may initialize objects with static storage duration with constant expressions. And an expression with a call of malloc is not a constant expression.

So you need for example initialize the pointer as a null pointer like for example

static double *first = NULL;

and then call the function malloc in a function

if ( first == NULL ) first = (double *)malloc( N*N*sizeof( double ) );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.