Home > OS >  C basic question about defining static arrays and macros
C basic question about defining static arrays and macros

Time:12-29

I'm trying to define some arrays, here is my code:

#include <stdio.h>
#define int N=50;
#define size (N 2)*(N 2)
#define IX(i,j) ((i) (N 2)*(j))
#define SWAP(x0,x) {float *tmp=x0;x0=x;x=tmp;}

int main()
{
    static float u[size], v[size], u_prev[size], v_prev[size];
}

It says, when compiling:

|9|error: storage size of 'u' isn't constant|

if i change #define int N=50; to #define N 50 it says:

|2|error: expected ';', ',' or ')' before numeric constant|

Does somebody have an answer?

CodePudding user response:

Lets take u[size] as an example. If you start replacing the text with the macro the following will happen:

-> u[size]

-> u[(N 2)*(N 2)]

and that's it... That is it is not a constant, because N is not a constant (and undefined).

Now if we take your suggested answer, it should work. What I think you may have is #define N 50;. Notice the semicolon at the end. If you replace the last line, above, with that macro, the new result will be:

-> u[(50; 2)*(50; 2)]

which is invalid syntax.

The last thing you're missing is a type from your declaration. Might want to put int or some other type after static.

It seems like you're missing the fundamentals of macros. Your first macro #define int N=50 is bad, ver bad. Anytime the word int is mentioned in the code below it, it will get replaced with N=50.

  • Related