can i initialize a char array variable like this

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thinktwice

    can i initialize a char array variable like this

    char a[256] = { 0 }
    is it ok?

  • Flo

    #2
    Re: can i initialize a char array variable like this

    Yes, it is ok form a syntax point of view if you append a semicolon.

    char a[256] = { 0 } ;

    However that initialises only the first element to 0, all other
    elements are left uninitialized, what is probably not what you have
    intended to do. If you like to initialize the whole array, you have to
    initialize each element as in

    char a[3] = { 'a', 'b', 0 } ;

    For your huge array, i would use a loop or memset to initialize it.

    Greetings

    Flo

    Comment

    • Bart

      #3
      Re: can i initialize a char array variable like this

      Flo wrote:
      Yes, it is ok form a syntax point of view if you append a semicolon.
      >
      char a[256] = { 0 } ;
      >
      However that initialises only the first element to 0, all other
      elements are left uninitialized, what is probably not what you have
      intended to do.
      Not so. All other elements will be _default initialized_ which is not
      the same as _uninitialized_ . The default initalizer for built-in types
      is zero.

      Regards,
      Bart.

      Comment

      • Victor Bazarov

        #4
        Re: can i initialize a char array variable like this

        Flo wrote:
        Yes, it is ok form a syntax point of view if you append a semicolon.
        >
        char a[256] = { 0 } ;
        >
        However that initialises only the first element to 0, all other
        elements are left uninitialized, [...]
        That is incorrect. All elements that are not given an explicit
        initialiser are *zero-initialised*.

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask


        Comment

        • Clark S. Cox III

          #5
          Re: can i initialize a char array variable like this

          Flo wrote:
          Yes, it is ok form a syntax point of view if you append a semicolon.
          >
          char a[256] = { 0 } ;
          >
          However that initialises only the first element to 0, all other
          elements are left uninitialized,
          That is simply not true, all of the other elements are default
          initialized; which for integer types (like char) means that they are all
          initialized to zero. If your compiler does not do this, then it is broken.

          In the following:

          char a[256] = {1};

          The first element is initialized to (1), while all others are
          initialized to (0).
          what is probably not what you have
          intended to do. If you like to initialize the whole array, you have to
          initialize each element as in
          >
          char a[3] = { 'a', 'b', 0 } ;
          >
          For your huge array, i would use a loop or memset to initialize it.
          I wouldn't.


          --
          Clark S. Cox III
          clarkcox3@gmail .com

          Comment

          • Kai-Uwe Bux

            #6
            Re: can i initialize a char array variable like this

            Flo wrote:
            Yes, it is ok form a syntax point of view if you append a semicolon.
            >
            char a[256] = { 0 } ;
            >
            However that initialises only the first element to 0, all other
            elements are left uninitialized,
            really?

            [8.5.1/7] If there are fewer initializers in the list than there are members
            in the aggregate, then each member not explicitly initialized shall be
            value-initialized (8.5).

            [8.5/5] ...
            To value-initialize an object of type T means:
            ? if T is a class type (clause 9) with a user-declared constructor (12.1),
            then the default constructor for T is called (and the initialization is
            ill-formed if T has no accessible default constructor);
            ? if T is a non-union class type without a user-declared constructor, then
            every non-static data member and base-class component of T is
            value-initialized;
            ? if T is an array type, then each element is value-initialized;
            ? otherwise, the object is zero-initialized

            what is probably not what you have intended to do. If you like to
            initialize the whole array, you have to initialize each element as in
            >
            char a[3] = { 'a', 'b', 0 } ;
            >
            For your huge array, i would use a loop or memset to initialize it.

            Best

            Kai-Uwe Bux

            Comment

            • hankssong

              #7
              Re: can i initialize a char array variable like this

              if you want to initialize the char array with 0,
              you can use a another way:
              char a[256] = "";

              thinktwice 写道:
              char a[256] = { 0 }
              is it ok?

              Comment

              • Pete Becker

                #8
                Re: can i initialize a char array variable like this

                hankssong wrote:
                >
                >>char a[256] = { 0 }
                >>is it ok?
                >
                if you want to initialize the char array with 0,
                you can use a another way:
                char a[256] = "";
                >
                That will set the first character to 0, which is fine if that's what's
                needed. It's different from the first version, which sets all the
                characters to 0.

                --

                -- Pete

                Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
                For more information about this book, see www.petebecker.com/tr1book.

                Comment

                • Ron Natalie

                  #9
                  Re: can i initialize a char array variable like this

                  Flo wrote:
                  Yes, it is ok form a syntax point of view if you append a semicolon.
                  >
                  char a[256] = { 0 } ;
                  >
                  However that initialises only the first element to 0, all other
                  elements are left uninitialized, what is probably not what you have
                  intended to do.
                  Untrue! When you specify fewer initializers to an aggregate than there
                  are elements, the rest are default initialized.
                  >

                  Comment

                  • Noah Roberts

                    #10
                    Re: can i initialize a char array variable like this


                    Flo wrote:
                    Yes, it is ok form a syntax point of view if you append a semicolon.
                    >
                    char a[256] = { 0 } ;
                    >
                    However that initialises only the first element to 0, all other
                    elements are left uninitialized, what is probably not what you have
                    intended to do. If you like to initialize the whole array, you have to
                    initialize each element as in
                    >
                    char a[3] = { 'a', 'b', 0 } ;
                    >
                    For your huge array, i would use a loop or memset to initialize it.
                    WRONG!!!

                    Ok, I have nothing new to add, I just wanted to join in with the rest
                    of the hecklers and make sure you understand how very mistaken you
                    really are.

                    Comment

                    • Old Wolf

                      #11
                      Re: can i initialize a char array variable like this

                      Pete Becker wrote:
                      hankssong wrote:
                      >char a[256] = { 0 }
                      >is it ok?
                      if you want to initialize the char array with 0,
                      you can use a another way:
                      char a[256] = "";
                      >
                      That will set the first character to 0, which is fine if that's what's
                      needed. It's different from the first version, which sets all the
                      characters to 0.
                      Huh? When initializing an aggregate, all members get
                      initialized.

                      In this particular case, "" has identical meaning to { '\0' },
                      and all members of the array will be set to 0.

                      Comment

                      • Pete Becker

                        #12
                        Re: can i initialize a char array variable like this

                        Old Wolf wrote:
                        Pete Becker wrote:
                        >
                        >>hankssong wrote:
                        >>
                        >>>>char a[256] = { 0 }
                        >>>>is it ok?
                        >>>
                        if you want to initialize the char array with 0,
                        you can use a another way:
                        char a[256] = "";
                        >>
                        >>That will set the first character to 0, which is fine if that's what's
                        >>needed. It's different from the first version, which sets all the
                        >>characters to 0.
                        >
                        >
                        Huh? When initializing an aggregate, all members get
                        initialized.
                        >
                        In this particular case, "" has identical meaning to { '\0' },
                        and all members of the array will be set to 0.
                        >
                        Citation, please? 8.5.2 does not say that. This is not "aggregate
                        initialization, " so 8.5.1 does not apply.

                        --

                        -- Pete

                        Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
                        For more information about this book, see www.petebecker.com/tr1book.

                        Comment

                        • Kai-Uwe Bux

                          #13
                          Re: can i initialize a char array variable like this

                          Pete Becker wrote:
                          Old Wolf wrote:
                          >Pete Becker wrote:
                          >>
                          >>>hankssong wrote:
                          >>>
                          >>>>>char a[256] = { 0 }
                          >>>>>is it ok?
                          >>>>
                          >if you want to initialize the char array with 0,
                          >you can use a another way:
                          >char a[256] = "";
                          >>>
                          >>>That will set the first character to 0, which is fine if that's what's
                          >>>needed. It's different from the first version, which sets all the
                          >>>characters to 0.
                          >>
                          >>
                          >Huh? When initializing an aggregate, all members get
                          >initialized.
                          >>
                          >In this particular case, "" has identical meaning to { '\0' },
                          >and all members of the array will be set to 0.
                          >>
                          >
                          Citation, please? 8.5.2 does not say that.
                          I just read 8.5.2. It says: successive characters of the string-literal
                          initialize the members of the array; but I cannot find anything therein
                          that specifies what happens if the character array is longer than the
                          string literal (including the terminating 0). Is it up to the
                          implementation?
                          This is not "aggregate initialization, " so 8.5.1 does not apply.
                          That, I can see.


                          Best

                          Kai-Uwe Bux

                          Comment

                          • Pete Becker

                            #14
                            Re: can i initialize a char array variable like this

                            Kai-Uwe Bux wrote:
                            >
                            I just read 8.5.2. It says: successive characters of the string-literal
                            initialize the members of the array; but I cannot find anything therein
                            that specifies what happens if the character array is longer than the
                            string literal (including the terminating 0). Is it up to the
                            implementation?
                            >
                            Seems like it. If the char array is initialized from a literal string
                            then it's holding a C-style string, and there's no reason to look at
                            anything after the terminating null character.

                            --

                            -- Pete

                            Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
                            For more information about this book, see www.petebecker.com/tr1book.

                            Comment

                            • Old Wolf

                              #15
                              Re: can i initialize a char array variable like this

                              Pete Becker wrote:
                              Old Wolf wrote:
                              Pete Becker wrote:
                              >hankssong wrote:
                              char a[256] = "";
                              >
                              >That will set the first character to 0, which is fine if that's what's
                              >needed. It's different from the first version, which sets all the
                              >characters to 0.
                              Huh? When initializing an aggregate, all members get
                              initialized.
                              >
                              Citation, please? 8.5.2 does not say that. This is not "aggregate
                              initialization, " so 8.5.1 does not apply.
                              You're right that 8.5.1 doesn't apply (despite this being the
                              initialization of an aggregate), and 8.5.2 doesn't mention the
                              characters after the 0-terminator.

                              I notice that 8.5.1#4 appears to conflict with 8.5.2#1. It says:

                              An array of unknown size initialized with a brace
                              enclosed initializerlist containing n initializers, where n
                              shall be greater than zero, is defined as having n
                              elements (8.3.4).

                              If the code is:
                              char *array[] = { "string" };

                              then it's clear that { "string" } is a brace-enclosed initializer
                              list containing 1 initializer, and array is defined as having
                              1 element. So, according to 8.5.1#4, the code
                              char array[] = { "string" };

                              should also try to define array as having 1 element, and
                              initialize that element with a string literal, which should
                              cause an error since a string literal isn't a valid initailizer
                              for a single char -- in exactly the same way that
                              int array[] = { "string" };

                              fails. I think 8.5.1#4 should have an explicit mention that
                              this clause doesn't apply to initialization of character arrays
                              from a string literal.

                              Comment

                              Working...