Domanda

I am trying to learn to program in C (not C++!). I've read about external variables, which should (according to the writer) give a nicer code. In order to use the external variables, I must #define them in the .h file, before I can use them in main.c file, using the extern command in front of the variable. I am trying to create an array in the .h file like this:

#define timeVals[4][2];
timeVals[0][0] = 7;
timeVals[0][1] = 45;
timeVals[1][0] = 8;
timeVals[1][1] = 15;
timeVals[2][0] = 9;
timeVals[2][1] = 30;
timeVals[3][0] = 10;
timeVals[3][1] = 25;

(it's a clock I'm trying to make, simple program in console). The first column indicates hours and the second indicates minutes. In my main I have written

extern int timeVals[][];

but I get an error telling me that " expected identifier or '(' before '[' token|" and I can't see what the issue is... any ideas or advices? I am using the .h file to learn how to use external variables, so I can't move the values back into main.c

È stato utile?

Soluzione

First, this:

#define timeVals[4][2];

Is a confusion. You mean this:

int timeVals[4][2];

Put that in your .h file, then in your .c file, something like this:

int timeVals[4][2] = {
  { 1, 2 }, // ...
};

That's how you initialize the entire array (any unspecified elements will be zero).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top