Thread: array initialization & C standard

  1. #1
    .
    Join Date
    Nov 2003
    Posts
    307

    array initialization & C standard

    Code:
    void foo(void)
    {
         int b[3]={0};
         char c[4]={0x0};
    	/* this point */
    }
    I cannot find in the C99 standard - at /* this point */ - what the value of the two initialized auto arrays must be. Section 6.7.8 of the standard does not seem to say. I know what my ANSI compliant compilers produce:
    Code:
    Breakpoint 1, foo () at foo.c:3
    3           int b[3]={0};
    (gdb) step
    4           char c[4]={0x0};
    (gdb) step
    5       }
    (gdb) print c
    $1 = "\000\000\000"
    (gdb) print b
    $2 = {0, 0, 0}
    Any direction or help please? Where does it say 'all elements are set to'?
    (c shows only three because of gdb, but the fourth is always zero as well). I've always used
    Code:
    char whatever[32]={0x0};
    on the assumption it was standard.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should look at para 21:
    Quote Originally Posted by ISO C99
    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.
    Para 10 states that static objects are initialized to zero.

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    Thanks - I'll find and read it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Constants Initialization
    By danlee58 in forum C Programming
    Replies: 19
    Last Post: 08-31-2008, 06:54 AM
  2. Can you set array contents on initialization?
    By Baron in forum C Programming
    Replies: 2
    Last Post: 12-12-2005, 08:01 PM
  3. Two-Dimensional Array Initialization (please help)
    By pompey4life in forum C Programming
    Replies: 2
    Last Post: 10-17-2005, 07:09 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Basic Array Initialization in C
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-18-2002, 06:41 PM