6

In C, what does a "near-initialization" error mean?

For instance, the following will generate the error:

int a[9] = {{1,2,3},{2,3,4},{3,4,5}}

p.s Why does this example generate the error?

captaincurrie
  • 157
  • 1
  • 1
  • 7
  • 1
    You have an array of 9 elements but you're treating it like a 3x3 array. Even if they take the same amount of space in memory, you need to change the array definition or the initialisation. Also, it's not an error, likely just a warning. – AntonH May 01 '14 at 19:36
  • 4
    60s of googling [*"The compiler is giving you one warning. It's a two-line warning; the second line is telling you the location "near" where the warning was encountered."*](http://www.microchip.com/forums/m463491.aspx). Really... There also probably is no hyphen. You misunderstood the compiler error. @AntonH Care to write a proper answer combining both comments? You were 1st to solve it =) – luk32 May 01 '14 at 19:38
  • 1
    "near" is English for close by, in close proximity. In other words, "the error is located near the initializer". It is. – Hans Passant May 01 '14 at 19:42
  • @luk32 My google search only took 50 secs :) – AntonH May 01 '14 at 19:42
  • @AntonH Still I feel it is worth of answering in a proper manner and you were 1st =). – luk32 May 01 '14 at 19:43
  • @luk32 Wrote it up, let me know if I missed anything in my answer. – AntonH May 01 '14 at 19:47

1 Answers1

10

To combine my and @luk32's comments (edit: and @hans-passant).

Your error isn't so much an error, as it is a warning that you have a potential problem. It is near (as in, close by) the element a (there is no hyphen betyween "near" and "initialization", so the warning is near the element mentionned in the warning message; a "near-initialization" would mean that the element was almost but not quite initialised, which makes no sense).

int a[9] = {{1,2,3},{2,3,4},{3,4,5}}

Basically, you have a 1D array of size 9. But in your initialisation, you are treating it like a 2D 3x3 array. While they take up the same amount of space in memory, they are treated a little differently.

To resolve the problem, you would have to either change the definition:

int a[3][3] = {{1,2,3},{2,3,4},{3,4,5}}

Or the initialisation:

int a[9] = {1,2,3,2,3,4,3,4,5}

Informational link:

Provided by @luk32: http://www.microchip.com/forums/m463491.aspx

AntonH
  • 6,359
  • 2
  • 30
  • 40