Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c - What is "near-initialization"?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...