array initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arne.muller@gmail.com

    #1

    array initialization

    Hello,

    I was wondering if the following statement will initialize all the 6
    elements with NULL:

    void function() {
    int *list[6] = {NULL};
    . ..
    }

    what I want is an array with 6 pointers to int that are initialized
    with NULL. Or do I've to do

    int *list[6] = {NULL, NULL, NULL, NULL, NULL, NULL} ?

    thanks for your help,
    +kind regards,

    Arne

  • pete

    #2
    Re: array initialization

    arne.muller@gma il.com wrote:
    >
    Hello,
    >
    I was wondering if the following statement will initialize all the 6
    elements with NULL:
    >
    void function() {
    int *list[6] = {NULL};
    . ..
    }
    Yes, it will.
    A short initializer for an array,
    means that all of the elements
    following the initialized one or initialized ones,
    are initialized with a value of zero,
    and zero means the same thing as NULL
    when used to initialize a pointer.
    what I want is an array with 6 pointers to int that are initialized
    with NULL. Or do I've to do
    >
    int *list[6] = {NULL, NULL, NULL, NULL, NULL, NULL} ?
    No, you don't.

    --
    pete

    Comment

    • assiss

      #3
      Re: array initialization

      int *list[6] = {NULL};
      list[0]=NULL, then the other 5 elements will be initialized with 0.

      ONLY if you are sure that NULL equals to 0.

      Thu, 24 Aug 2006 15:49:10 -0700,arne.mul ler wrote:
      Hello,
      >
      I was wondering if the following statement will initialize all the 6
      elements with NULL:
      >
      void function() {
      int *list[6] = {NULL};
      . ..
      }
      >
      what I want is an array with 6 pointers to int that are initialized
      with NULL. Or do I've to do
      >
      int *list[6] = {NULL, NULL, NULL, NULL, NULL, NULL} ?
      >
      thanks for your help,
      +kind regards,
      >
      Arne

      Comment

      • Eric Sosman

        #4
        Re: array initialization

        assiss wrote:
        int *list[6] = {NULL};
        list[0]=NULL, then the other 5 elements will be initialized with 0.
        >
        ONLY if you are sure that NULL equals to 0.
        I am sure that NULL equals 0. Always. On every
        Standard-conforming C implementation ever built. Amen.

        --
        Eric Sosman
        esosman@acm-dot-org.invalid

        Comment

        • Robert Gamble

          #5
          Re: array initialization

          Eric Sosman wrote:
          assiss wrote:
          >
          int *list[6] = {NULL};
          list[0]=NULL, then the other 5 elements will be initialized with 0.

          ONLY if you are sure that NULL equals to 0.
          >
          I am sure that NULL equals 0. Always. On every
          Standard-conforming C implementation ever built. Amen.
          assiss may be confusing the *value* of a null pointer constant with the
          *representation * of a null pointer constant. He would be well-advised
          to re-read Chapter 5 of the FAQ <http://c-faq.com/>.

          Robert Gamble

          Comment

          • assiss

            #6
            Re: array initialization

            Robert Gamble wrote:
            Eric Sosman wrote:
            >assiss wrote:
            >>
            >>int *list[6] = {NULL};
            >>list[0]=NULL, then the other 5 elements will be initialized with 0.
            >>>
            >>ONLY if you are sure that NULL equals to 0.
            > I am sure that NULL equals 0. Always. On every
            >Standard-conforming C implementation ever built. Amen.
            >
            assiss may be confusing the *value* of a null pointer constant with the
            *representation * of a null pointer constant. He would be well-advised
            to re-read Chapter 5 of the FAQ <http://c-faq.com/>.
            >
            Robert Gamble
            >
            thanks. I do forget someting about NULL.

            Comment

            • CBFalconer

              #7
              Re: array initialization

              Eric Sosman wrote:
              assiss wrote:
              >
              >int *list[6] = {NULL};
              >list[0]=NULL, then the other 5 elements will be initialized with 0.
              >>
              >ONLY if you are sure that NULL equals to 0.
              >
              I am sure that NULL equals 0. Always. On every
              Standard-conforming C implementation ever built. Amen.
              Oh? Consider:

              #include <stdio.h>

              int main(void);
              int n = 0;

              if (n == NULL) puts('You are right');
              else puts('You are wrong');
              return 0;
              }

              Note that I did NOT write "if (0 == NULL)"

              --
              Chuck F (cbfalconer@yah oo.com) (cbfalconer@mai neline.net)
              Available for consulting/temporary embedded and systems.
              <http://cbfalconer.home .att.netUSE maineline address!


              Comment

              • arne.muller@gmail.com

                #8
                Re: array initialization

                Thanks for your replies! Fromù what I read in the FAQ one can use 0 or
                NULL (macro) - but the little prog below just gives me a segmentation
                fault ;-(

                kind regards,

                Arne

                CBFalconer wrote:
                Eric Sosman wrote:
                assiss wrote:
                int *list[6] = {NULL};
                list[0]=NULL, then the other 5 elements will be initialized with 0.
                >
                ONLY if you are sure that NULL equals to 0.
                I am sure that NULL equals 0. Always. On every
                Standard-conforming C implementation ever built. Amen.
                >
                Oh? Consider:
                >
                #include <stdio.h>
                >
                int main(void);
                int n = 0;
                >
                if (n == NULL) puts('You are right');
                else puts('You are wrong');
                return 0;
                }
                >
                Note that I did NOT write "if (0 == NULL)"
                >
                --
                Chuck F (cbfalconer@yah oo.com) (cbfalconer@mai neline.net)
                Available for consulting/temporary embedded and systems.
                <http://cbfalconer.home .att.netUSE maineline address!

                Comment

                • Eric Sosman

                  #9
                  Re: array initialization

                  CBFalconer wrote:
                  Eric Sosman wrote:
                  >
                  >>assiss wrote:
                  >>
                  >>
                  >>>int *list[6] = {NULL};
                  >>>list[0]=NULL, then the other 5 elements will be initialized with 0.
                  >>>
                  >>>ONLY if you are sure that NULL equals to 0.
                  >>
                  > I am sure that NULL equals 0. Always. On every
                  >>Standard-conforming C implementation ever built. Amen.
                  >
                  >
                  Oh? Consider:
                  >
                  #include <stdio.h>
                  >
                  int main(void);
                  int n = 0;
                  >
                  if (n == NULL) puts('You are right');
                  else puts('You are wrong');
                  return 0;
                  }
                  >
                  Note that I did NOT write "if (0 == NULL)"
                  ... and I for my part did NOT write "NULL equals zero."

                  (Also, it looks like your double-quote key is functioning
                  erratically: Sometimes it omits the left or right half -- hard
                  to tell which -- of its symbol. Have it checked before it gets
                  even worse, and maybe catches fire. ;-)

                  Back to the thread: assiss, Chuck and I have been having a
                  little fun at your expense, and I apologize. Here's the scoop:
                  Every variable that's "initialize d in part" is "initialize d in
                  full." If you provide an initializer for just a few elements
                  of an array or struct, all the other elements are initialized
                  for you. The elements you don't initialize explicitly are all
                  initialized to "zeroes of the proper type:" 0.0 for double,
                  '\0' for char, (char*)0 for char* pointers, and so on. (Union
                  objects are a little different because they can hold only one
                  value at a time, so when a union is initialized this way what
                  happens is that its first element is given a zero of the type
                  appropriate to that element.) This all works even on machines
                  where the various kinds of zero and NULL might not be represented
                  as all-bits-zero; it's the compiler's business to make it work.

                  What variables are initialized this way? Two kinds: those
                  with static storage duration (variables outside functions plus
                  `static' variables inside functions), and any variables for
                  which you provide a partial initializer:

                  int a; /* a == 0 */
                  int b = 3; /* b == 3 */
                  int c[4] = {1,2}; /* c == 1,2,0,0 */
                  struct s {
                  int x, y;
                  } d = { 1 }; /* d.x == 1, d.y == 0 */
                  union u {
                  char *cp;
                  int i;
                  float f;
                  } e; /* e.cp == (char*)0 */

                  void f(void) {
                  static int fa; /* fa == 0 */
                  int fc[4] = {9}; /* fc == 9,0,0,0 */
                  struct s fd = { 2 }; /* fd.x == 2, fd.y == 0 */
                  int v; /* NOT INITIALIZED */
                  }

                  The newer "C99" version of the Standard allows some fancier
                  forms of initializers (for example, you can provide explicit
                  initializers for elements [5],[1],[9] of an array, in that order),
                  but even then the rule holds: Initialize any part of something,
                  and all the parts you don't initialize receive zeroes.

                  --
                  Eric Sosman
                  esosman@acm-dot-org.invalid

                  Comment

                  • assiss

                    #10
                    Re: array initialization

                    Eric Sosman wrote:
                    CBFalconer wrote:
                    >Eric Sosman wrote:
                    >>
                    >>assiss wrote:
                    >>>
                    >>>
                    >>>int *list[6] = {NULL};
                    >>>list[0]=NULL, then the other 5 elements will be initialized with 0.
                    >>>>
                    >>>ONLY if you are sure that NULL equals to 0.
                    >>>
                    >> I am sure that NULL equals 0. Always. On every
                    >>Standard-conforming C implementation ever built. Amen.
                    >>
                    >>
                    >Oh? Consider:
                    >>
                    >#include <stdio.h>
                    >>
                    >int main(void);
                    > int n = 0;
                    >>
                    > if (n == NULL) puts('You are right');
                    > else puts('You are wrong');
                    > return 0;
                    >}
                    >>
                    >Note that I did NOT write "if (0 == NULL)"
                    >
                    ... and I for my part did NOT write "NULL equals zero."
                    >
                    (Also, it looks like your double-quote key is functioning
                    erratically: Sometimes it omits the left or right half -- hard
                    to tell which -- of its symbol. Have it checked before it gets
                    even worse, and maybe catches fire. ;-)
                    >
                    Back to the thread: assiss, Chuck and I have been having a
                    little fun at your expense, and I apologize. Here's the scoop:
                    Every variable that's "initialize d in part" is "initialize d in
                    full." If you provide an initializer for just a few elements
                    of an array or struct, all the other elements are initialized
                    for you. The elements you don't initialize explicitly are all
                    initialized to "zeroes of the proper type:" 0.0 for double,
                    '\0' for char, (char*)0 for char* pointers, and so on. (Union
                    objects are a little different because they can hold only one
                    value at a time, so when a union is initialized this way what
                    happens is that its first element is given a zero of the type
                    appropriate to that element.) This all works even on machines
                    where the various kinds of zero and NULL might not be represented
                    as all-bits-zero; it's the compiler's business to make it work.
                    >
                    What variables are initialized this way? Two kinds: those
                    with static storage duration (variables outside functions plus
                    `static' variables inside functions), and any variables for
                    which you provide a partial initializer:
                    >
                    int a; /* a == 0 */
                    int b = 3; /* b == 3 */
                    int c[4] = {1,2}; /* c == 1,2,0,0 */
                    struct s {
                    int x, y;
                    } d = { 1 }; /* d.x == 1, d.y == 0 */
                    union u {
                    char *cp;
                    int i;
                    float f;
                    } e; /* e.cp == (char*)0 */
                    >
                    void f(void) {
                    static int fa; /* fa == 0 */
                    int fc[4] = {9}; /* fc == 9,0,0,0 */
                    struct s fd = { 2 }; /* fd.x == 2, fd.y == 0 */
                    int v; /* NOT INITIALIZED */
                    }
                    >
                    The newer "C99" version of the Standard allows some fancier
                    forms of initializers (for example, you can provide explicit
                    initializers for elements [5],[1],[9] of an array, in that order),
                    but even then the rule holds: Initialize any part of something,
                    and all the parts you don't initialize receive zeroes.
                    >
                    Thanks very much.
                    I check out ansi_c99 and find that I have some wrong understandings
                    about initializations .

                    Comment

                    • CBFalconer

                      #11
                      Re: array initialization

                      arne.muller@gma il.com wrote:
                      >
                      Thanks for your replies! Fromù what I read in the FAQ one can use
                      0 or NULL (macro) - but the little prog below just gives me a
                      segmentation fault ;-(
                      Please do not top-post. Some details and reasons in the links
                      below.

                      --
                      Some informative links:
                      news:news.annou nce.newusers
                      Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!






                      Comment

                      • Frederick Gotham

                        #12
                        Re: array initialization

                        Eric Sosman posted:
                        union u {
                        char *cp;
                        int i;
                        float f;
                        } e; /* e.cp == (char*)0 */

                        Are you saying that zero-intialisation for a union-type results in zero-
                        initialisation of its first member?

                        --

                        Frederick Gotham

                        Comment

                        • Robert Gamble

                          #13
                          Re: array initialization

                          Frederick Gotham wrote:
                          Eric Sosman posted:
                          >
                          union u {
                          char *cp;
                          int i;
                          float f;
                          } e; /* e.cp == (char*)0 */
                          >
                          >
                          Are you saying that zero-intialisation for a union-type results in zero-
                          initialisation of its first member?
                          I think he is saying that a static variable of union type has it's
                          first member initialized to zero which is correct.

                          Robert Gamble

                          Comment

                          • Eric Sosman

                            #14
                            Re: array initialization



                            Frederick Gotham wrote On 08/25/06 10:16,:
                            Eric Sosman posted:
                            >
                            >
                            > union u {
                            > char *cp;
                            > int i;
                            > float f;
                            > } e; /* e.cp == (char*)0 */
                            >
                            >
                            >
                            Are you saying that zero-intialisation for a union-type results in zero-
                            initialisation of its first member?
                            Give the man a cigar! (Section 6.7.8, paragraph 10.)

                            Notes: (1) The example above came from a larger example
                            in which it was clear that the declaration was at file scope,
                            hence had static storage duration, and hence was initialized
                            even without an explicit initializer. (2) It's actually not
                            the union's first element that gets initialized, but its
                            first *named* element.

                            union u2 {
                            int : 4; /* unnamed bit-field */
                            int : 13; /* another one */
                            char *cp; /* first named element */
                            double d;
                            } f; /* f.cp == (char*)0 */

                            Unnamed elements in unions seem pretty useless, but as far
                            as I can see they're permitted.

                            --
                            Eric.Sosman@sun .com

                            Comment

                            • Flash Gordon

                              #15
                              Re: array initialization

                              assiss wrote:

                              Your reply belongs under (or interspersed with) the text you are
                              replying to. NEVER above. I've corrected it this time.
                              Thu, 24 Aug 2006 15:49:10 -0700,arne.mul ler wrote:
                              >
                              >Hello,
                              >>
                              >I was wondering if the following statement will initialize all the 6
                              >elements with NULL:
                              >>
                              >void function() {
                              > int *list[6] = {NULL};
                              > . ..
                              >}
                              <snip>
                              int *list[6] = {NULL};
                              list[0]=NULL, then the other 5 elements will be initialized with 0.
                              >
                              ONLY if you are sure that NULL equals to 0.
                              Wrong. The C standard defines that the other five elements will be
                              initialised to null pointers (not NULL which is a macro name). This will
                              be the case however null pointers are represented.
                              --
                              Flash Gordon.

                              Comment

                              Working...