0

I am trying to initialize a 2D array named tiles consisting of int like this:

for (int i=0; i<d; i++)
    {
        for (int j=0; j<d; j++)
        {
            int tiles[d][d];
            tiles[i][j] = (d**2 - 1)-(j+i);
        }

But I get this error:

fifteen.c:192:34: error: indirection requires pointer operand ('int' 
invalid)
            int tiles[i][j] = (d**2 - 1)-(j+i);

(the error highlights the last *2).

I do not know what exactly the problem with d**2 is, which I meant to be d^2. I have checked this answer, which means I know that math declarations can be used to initialize arrays, so I do not know what the matter might be.

Thank you all

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
cs-laz
  • 69
  • 1
  • 10
  • 7
    there's no power operator in C. Try `d*d`. in your case, you get away with a simple multiplication :) – Jean-François Fabre Aug 25 '17 at 22:07
  • I thought I had used ** before; just like I thought I had found all the other possible duplicate questions as well. I will check again, thank you!! – cs-laz Aug 25 '17 at 22:16
  • you're welcome. maybe you used that in python or Fortran in another coder's life. – Jean-François Fabre Aug 25 '17 at 22:17
  • Using `pow` here would be very wrong. Also, you're not *initializing* the array with an initializer, you're just assigning values. – Antti Haapala -- Слава Україні Aug 26 '17 at 07:26
  • Off-topic: you'd better define array `tiles` _outside_ of your loops, if you intend to do anything useful with it. – Ruud Helderman Aug 31 '17 at 13:57
  • Hi @RuudHelderman, thanks for writing! I think I know what you mean.. are you talking about the fact that everything declared inside a loop is "deleted" from memory once the loop is over? The code in this question had to be changed because after the loop above there were 2 other loops using `tiles`, where `tiles` had to be "redeclared" because it was as if `tiles` had never existed in that program. Something that shocked me actually, I would really enjoy knowing why this happens. Thanks again!! – cs-laz Aug 31 '17 at 17:25
  • 1
    @cs-laz In general, keep an eye on the _lexical scope_ of each variable; that is the closest pair of braces `{...}` inside which you declare that variable. Any statement _outside_ that scope which refers to the variable by its name, will cause a compiler error. In your particular case, I'd have to see the code and the exact error message. – Ruud Helderman Aug 31 '17 at 20:46

0 Answers0