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