0

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

Zuenonentu
  • 302
  • 1
  • 3
  • 16
  • For an extern array example see, e.g. http://stackoverflow.com/questions/7670816/create-extern-char-array-in-c – Brandin Mar 02 '14 at 13:42

1 Answers1

2

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).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • So there isn't any way in which I can define and initialize variables in the .h file? (I read in a book that it sohuld be possible, so just wondering) – Zuenonentu Mar 02 '14 at 12:16
  • Not in the way you are thinking. Hopefully your book has some concrete examples that work. – John Zwinck Mar 02 '14 at 12:18