These are some of the rules on initialization; we'll learn a few more later as we learn about a few more data types.
If you don't feel like memorizing the rules for default initialization, just go ahead and explicitly initialize everything you care about.
Earlier we said that C is quite general in its treatment of expressions: anywhere you can use an expression, you can use any expression. Here's an exception to that rule: in an initialization of an external or static variable (strictly speaking, any variable of static duration; generally speaking, any global variable or local static variable), the initializer must be a constant expression, with value determinable at compile time, without calling any functions. (This rule is easy to understand: since these initializations happen conceptually at compile time, before the program starts running, there's no way for a function call--that is, some run-time action--to be involved.)
page 86
It probably won't concern you right away, but it turns out that there's another exception about the allowable expressions in initializers: in the brace-enclosed list of initializers for an array, all of the expressions must be constant expressions (even for local arrays).
There is an error in some printings: if there are fewer explicit initializers than required for an array, the others will be initialized to zero, for external, static, and automatic (local) arrays. (When an automatic array has no initializers at all, then it contains garbage, just as simple automatic variables do.)
If the initialization
char pattern[] = "ould";makes sense to you, you're fine. But if the statement that
char pattern[] = "ould";is equivalent to
char pattern[] = { 'o', 'u', 'l', 'd', '\0' };bothers you at all, study it until it makes sense. Also, note that a character array which seems to contain (for example) four characters actually contains five, because of the terminating '\0'.
Read sequentially: prev next up top
This page by Steve Summit // Copyright 1995, 1996 // mail feedback