Partly initialized array default values in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jirik
    New Member
    • Jun 2007
    • 4

    Partly initialized array default values in C

    Hi, I have a question concerning partly initialized array. I'll give an example of code:

    int array[20] = {[10] = 55};

    Does compiler grants me any assurance, that other values (except with index 10) will have some particular value? I've read, that when using C99, other values will be 0, but what if I don't specify any standard?
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by jirik
    Hi, I have a question concerning partly initialized array. I'll give an example of code:

    int array[20] = {[10] = 55};

    Does compiler grants me any assurance, that other values (except with index 10) will have some particular value? I've read, that when using C99, other values will be 0, but what if I don't specify any standard?
    Huh? What do you mean you don't specify any standard?

    If you specify an initialiser, even if it is empty (i.e. {}) it will default the uninitialised values to 0 or NULL (NULL doen't necessarly mean 0 on all CPUs).


    Adrian

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      What kind of code is this??

      [code=c]
      int array[20] = {[10] = 55};
      [/code]

      It's not C or C++.

      Comment

      • jirik
        New Member
        • Jun 2007
        • 4

        #4
        Originally posted by AdrianH
        Huh? What do you mean you don't specify any standard?
        Adrian
        I meant if compile it without -std switch

        Comment

        • jirik
          New Member
          • Jun 2007
          • 4

          #5
          Originally posted by weaknessforcats
          What kind of code is this??

          [code=c]
          int array[20] = {[10] = 55};
          [/code]

          It's not C or C++.
          Sorry, I thought it'd be much clearer, if I didn't specify anything else, just this declaration of an array.. I guess, this should be a correct snippet of C code

          Comment

          • jirik
            New Member
            • Jun 2007
            • 4

            #6
            Originally posted by AdrianH
            If you specify an initialiser, even if it is empty (i.e. {}) it will default the uninitialised values to 0 or NULL (NULL doen't necessarly mean 0 on all CPUs).
            Adrian
            Thanks, this is just what I wanted to hear

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by weaknessforcats
              What kind of code is this??

              [code=c]
              int array[20] = {[10] = 55};
              [/code]

              It's not C or C++.
              It is C from the C99 standard. You can also initialialise parts of structs in a similar way:
              [code=c]
              struct A {
              int a;
              char b[30];
              double c;
              char d[30];
              };

              struct A obj = { .b = "Hello", .d = { [29] = '\0' } };
              [/code]

              Anything not initialised is initialised to 0 or NULL. So initialising obj.d[29] to '\0' doesn't save you from not initialising obj.d[0-28].

              However, if you do not specifiy an initialiser list, the the elements are not initialised UNLESS it is not automatic storage, in which case it is set to 0 or NULL.

              I found this to be an interesting read.


              Adrian

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by AdrianH
                It is C from the C99 standard. You can also initialialise parts of structs in a similar way:
                Obviously I know only K&R C. Further research reveals Visual Studio.NET 2005 doesn't know about it either although there are various C99 doc pages all marked "unsupporte d".

                I based my comment on Visual Studio failing to compile the snippet as either C or C++.

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Originally posted by weaknessforcats
                  Obviously I know only K&R C. Further research reveals Visual Studio.NET 2005 doesn't know about it either although there are various C99 doc pages all marked "unsupporte d".

                  I based my comment on Visual Studio failing to compile the snippet as either C or C++.
                  Hey, a standard is a standard. If the compiler you are using doesn't conform to it, what can you do but not use the standard or use a compiler that does.


                  Adrian

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #10
                    Originally posted by AdrianH
                    Hey, a standard is a standard. If the compiler you are using doesn't conform to it, what can you do but not use the standard or use a compiler that does.


                    Adrian
                    I'm starting to hate all that mumbo jumbo about standards

                    Savage

                    Comment

                    • AdrianH
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1251

                      #11
                      Originally posted by Savage
                      I'm starting to hate all that mumbo jumbo about standards

                      Savage
                      Well, it is not so bad. At least the standards are incremental and not just willy nilly. ;)


                      Adrian

                      Comment

                      Working...