Connecting Tech Pros Worldwide Help | Site Map

Character array Initialization

Kannan
Guest
 
Posts: n/a
#1: Sep 9 '06
Hi,

I have question about character array initialization. In section 6.7.8
paragraph number 21, it's given that

"If there are fewer initializers in a brace-enclosed list than there
are elements or members of an aggregate, or fewer characters in a
string literal used to initialize an array of known size than there are
elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration."

My question is that, if I declare

char sTemp[5] = "";

Is it GUARANTEED that all the five elements of the array will be
initialized to '\0'? Or is it only guarantees that that sTemp[0] will
be initialized to '\0'?

Any help greatly appreciated.

Thank you for your time.

-Kannan

Keith Thompson
Guest
 
Posts: n/a
#2: Sep 9 '06

re: Character array Initialization


"Kannan" <riderchap@gmail.comwrites:
Quote:
Hi,
>
I have question about character array initialization. In section 6.7.8
paragraph number 21, it's given that
>
"If there are fewer initializers in a brace-enclosed list than there
are elements or members of an aggregate, or fewer characters in a
string literal used to initialize an array of known size than there are
elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration."
>
My question is that, if I declare
>
char sTemp[5] = "";
>
Is it GUARANTEED that all the five elements of the array will be
initialized to '\0'? Or is it only guarantees that that sTemp[0] will
be initialized to '\0'?
It certainly looks like it's guaranteed. The paragraph you quoted
refers specifically to astring literal used to initialize an array of
known size.

Why would you suspect that it isn't guaranteed?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Randall
Guest
 
Posts: n/a
#3: Sep 9 '06

re: Character array Initialization


Hello Kannan:

Sorry to add another question to yours... (see below)

Kannan wrote:
Quote:
Hi,
>
I have question about character array initialization. In section 6.7.8
paragraph number 21, it's given that
>
"If there are fewer initializers in a brace-enclosed list than there
are elements or members of an aggregate, or fewer characters in a
string literal used to initialize an array of known size than there are
elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration."
Does anybody know if the standard behavior is scope-specific? To
further clarify, is it portable to assume this:

/* Begin Program */
char Extern_sTemp[5] = ""; // all elements are null right?
static char Static_sTemp[5] = ""; // ditto?

int main( void )
{
char Auto_sTemp[5] = ""; // same here?
return 0;
}
/* End Program */
Quote:
>
My question is that, if I declare
>
char sTemp[5] = "";
>
Is it GUARANTEED that all the five elements of the array will be
initialized to '\0'? Or is it only guarantees that that sTemp[0] will
be initialized to '\0'?
For lack of a copy of the C standard (yes I should buy it) I have coded
around this problem in my production code by using memset() or the like
to initialize the appropriate memory to a "known" default value (like
'\0', or 0, or NULL) -- whichever you prefer). Usually it would look
like this:

#define STEMP_MAXSIZE 5
....
char sTemp[ STEMP_MAXSIZE ];
memset( sTemp, 0, STEMP_MAXSIZE); // my preferred way of doing it
....

/* OR */

char sTemp[5];
memset( sTemp, 0, sizeof(sTemp) ); // I like this version less

Quote:
Any help greatly appreciated.
>
Thank you for your time.
>
-Kannan
I really like the interesting point you bring up. I have looked for a
way of using memset() less to initialize contiguous areas of memory.

Below is a link to an article describing how such a simple use of
memset() can get ugly:

"Security Enhancements" (note: this article has a C++ bias)
http://www.informit.com/guides/conte...eqNum=202&rl=1

-Randall
Sr. Software Engineer
Lockheed Martin

Simon Biber
Guest
 
Posts: n/a
#4: Sep 9 '06

re: Character array Initialization


Randall wrote:
Quote:
Does anybody know if the standard behavior is scope-specific? To
further clarify, is it portable to assume this:
>
/* Begin Program */
char Extern_sTemp[5] = ""; // all elements are null right?
All 5 elements have value zero. They are null characters.
Quote:
static char Static_sTemp[5] = ""; // ditto?
Yes, initialised to exactly the same values.
Quote:
int main( void )
{
char Auto_sTemp[5] = ""; // same here?
Yes, initialised to exactly the same values.
Quote:
return 0;
}
/* End Program */
>
If there is an initialiser present, then the initialiser has the same
meaning no matter what the scope, storage type and linkage type.

If there is no initialiser, then the behaviour depends on the storage
type. Objects with static storage (including extern and static linkage,
and in any scope) are initialised to zeros, but objects with automatic
storage are not initialised and have indeterminate values.

char ExternLinkage_StaticStorage_FileScope[5];

static char NoLinkage_StaticStorage_FileScope[5];

void foo(void)
{
static char NoLinkage_StaticStorage_BlockScope[5];

char NoLinkage_AutomaticStorage_BlockScope[5];
}

Of these four definitions, the first three all have static storage, and
so they are automatically initialised to zeros. The last one has
automatic storage, and so it is not automatically initialised.

--
Simon.
goose
Guest
 
Posts: n/a
#5: Sep 9 '06

re: Character array Initialization


Randall wrote:
<snipped>
Quote:
#define STEMP_MAXSIZE 5
....
char sTemp[ STEMP_MAXSIZE ];
memset( sTemp, 0, STEMP_MAXSIZE); // my preferred way of doing it
....
>
/* OR */
>
char sTemp[5];
memset( sTemp, 0, sizeof(sTemp) ); // I like this version less
>
>
Your preferred way is prone to bugs when the
inevitable maintenance comes around 5 years
after you left the organisation.

My preferred way is:
memset (sTemp, 0, sizeof sTemp);

Using sizeof with unnecessary parentheses tends
to either tell the maintenance programmer that
sTemp is a typedef *OR* tell the beginner programmer
that sizeof is a function call.

But hey, different strokes ...

<snipped>

--
goose
Have I offended you? Send flames to root@localhost
real email: lelanthran at gmail dot com
website : www.lelanthran.com
Robert Gamble
Guest
 
Posts: n/a
#6: Sep 9 '06

re: Character array Initialization


Randall wrote:

[snip]
Quote:
For lack of a copy of the C standard (yes I should buy it)
Or you can download the free post-Standard draft which is essentially
the C99 Standard plus TC1 and TC2:
<www.open-std.org/JTC1/SC22/WG14/www/docs/n1124.pdf>

Robert Gamble

Snis Pilbor
Guest
 
Posts: n/a
#7: Sep 10 '06

re: Character array Initialization



Keith Thompson wrote:
Quote:
"Kannan" <riderchap@gmail.comwrites:
Quote:

Is it GUARANTEED that all the five elements of the array will be
initialized to '\0'? Or is it only guarantees that that sTemp[0] will
be initialized to '\0'?
>
It certainly looks like it's guaranteed. The paragraph you quoted
refers specifically to astring literal used to initialize an array of
known size.
>
Why would you suspect that it isn't guaranteed?
>
Hmm , this actually does come as a big surprise to me. And since you
asked, the reason is that it seems to be an unnecessary waste of
runtime. If for some bizarre reason I insist on having the whole array
of chars be zerod after the initializer, I can say so; far more often,
I couldn't care less what defaults come in the places beyond an initial
terminator. In the overwhelming majority of cases, one will not be
peeking ahead of the terminator in a string.

Closed Thread