Array Initialization

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

    Array Initialization

    I finally thought I had an understanding of multi dimensional arrays in C
    when I get this:

    #include <stdio.h>

    #define max_x 3
    #define max_y 5

    int array[max_x][max_y];

    main()
    {
    int x,y;

    for(x = 0; x < max_x; x++)
    for(y=0; y < max_y; y++)
    array[x][y] = x * 10 + y;

    for(y = 0; y < max_y; y++) {
    (void)printf("a rray[%d] ", y);
    for(x = 0; x < max_x; x++)
    (void)printf("% d ", array[x,y]);
    (void)printf("\ n");
    }

    return 0;
    }

    This program initializes to :
    array[0] 56164 56164 56164
    array[1] 56184 56184 56184
    array[2] 56204 56204 56204
    array[3] 56224 56224 56224
    array[4] 56244 56244 56244

    Could someone please help me figure out how this program arrives at such a
    large number?


  • Bigdakine

    #2
    Re: Array Initialization

    >Subject: Array Initialization[color=blue]
    >From: "Henry" henryhc@knology .net
    >Date: 9/2/03 3:27 PM Hawaiian Standard Time
    >Message-id: <vlagssbqf5q9f3 @corp.supernews .com>
    >
    >I finally thought I had an understanding of multi dimensional arrays in C
    >when I get this:
    >
    >#include <stdio.h>
    >
    >#define max_x 3
    >#define max_y 5
    >
    >int array[max_x][max_y];
    >
    >main()
    >{
    > int x,y;
    >
    > for(x = 0; x < max_x; x++)
    > for(y=0; y < max_y; y++)
    > array[x][y] = x * 10 + y;
    >
    > for(y = 0; y < max_y; y++) {
    > (void)printf("a rray[%d] ", y);
    > for(x = 0; x < max_x; x++)
    > (void)printf("% d ", array[x,y]);
    > (void)printf("\ n");
    > }
    >
    > return 0;
    >}
    >
    >This program initializes to :
    >array[0] 56164 56164 56164
    >array[1] 56184 56184 56184
    >array[2] 56204 56204 56204
    >array[3] 56224 56224 56224
    >array[4] 56244 56244 56244
    >
    >Could someone please help me figure out how this program arrives at such a
    >large number?
    >[/color]

    Heck, I'm interested to know how this even compiled.

    (void)printf("% d ", array[x,y]);

    Should be .. array[x][y]....

    Fortran dies hard, don't it? :-)

    Stuart
    Dr. Stuart A. Weinstein
    Ewa Beach Institute of Tectonics
    "To err is human, but to really foul things up
    requires a creationist"

    Comment

    • Nick Austin

      #3
      Re: Array Initialization

      On Tue, 2 Sep 2003 21:27:36 -0400, "Henry" <henryhc@knolog y.net>
      wrote:
      [color=blue]
      >I finally thought I had an understanding of multi dimensional arrays in C
      >when I get this:
      >
      >#include <stdio.h>
      >
      >#define max_x 3
      >#define max_y 5
      >
      >int array[max_x][max_y];
      >
      >main()
      >{
      > int x,y;
      >
      > for(x = 0; x < max_x; x++)
      > for(y=0; y < max_y; y++)
      > array[x][y] = x * 10 + y;
      >
      > for(y = 0; y < max_y; y++) {
      > (void)printf("a rray[%d] ", y);
      > for(x = 0; x < max_x; x++)
      > (void)printf("% d ", array[x,y]);[/color]

      (void)printf("% d ", array[x][y]);

      array[x,y] uses the comma operator and is the same as array[y].

      You're invoking UB because array[y] has type 'int *' and
      you're printing it with %d.
      [color=blue]
      > (void)printf("\ n");
      > }
      >
      > return 0;
      >}
      >
      >This program initializes to :
      >array[0] 56164 56164 56164
      >array[1] 56184 56184 56184
      >array[2] 56204 56204 56204
      >array[3] 56224 56224 56224
      >array[4] 56244 56244 56244
      >
      >Could someone please help me figure out how this program arrives at such a
      >large number?[/color]

      Nick.

      Comment

      • Brett Frankenberger

        #4
        Re: Array Initialization

        In article <vlagssbqf5q9f3 @corp.supernews .com>,
        Henry <henryhc@knolog y.net> wrote:[color=blue]
        >I finally thought I had an understanding of multi dimensional arrays in C
        >when I get this:
        >
        >int array[max_x][max_y];
        >
        > (void)printf("% d ", array[x,y]);[/color]

        array[x,y] is not the same thing as array[x][y].
        The expression:
        x,y
        is a comma expression whose value is the value of the second
        expression (in this case, y). The first expression (in this case, x)
        would be evaluated for its side effects; but in this case, there are
        none. So x,y is equivalent to just:
        y

        So you effectively have
        printf("%d ", array[y])

        array[y] is type (pointer to int). "%d" requires an int. So you're
        using an "int" format specifier to print a (pointer to int); the result
        is undefined. Change the code to:
        printf("%d ", array[x][y]);
        and you'll probably get what you expected.

        By the way, I'm curious what platform you are running on. (i.e. what
        compiler and what OS and what processor)? I'm somewhat surprised at
        the numbers you got. (It's all undefined, of course ... but on a
        typical PC platform, I'd have expected the actual result to be much
        larger than the numbers you got.)

        -- Brett

        Comment

        • Brett Frankenberger

          #5
          Re: Array Initialization

          In article <20030902214254 .22819.00000248 @mb-m23.aol.com>,
          Bigdakine <bigdakine@aol. comGetaGrip> wrote:[color=blue]
          >
          >Heck, I'm interested to know how this even compiled.
          > (void)printf("% d ", array[x,y]);[/color]

          Why wouldn't it? 'x,y' is a perfectly valid expression (equivalent to
          'y'), and array[y] doesn't violate any constraints (and would even be
          useful, defined behavior under some circumstances), so one wouldn't
          expect compilation to fail. Of course, array[y] is type (pointer to
          int), so attempting to print it with "%d" is undefined behavior.

          -- Brett

          Comment

          • Henry

            #6
            Re: Array Initialization

            > is a comma expression whose value is the value of the second[color=blue]
            > expression (in this case, y). The first expression (in this case, x)
            > would be evaluated for its side effects; but in this case, there are
            > none. So x,y is equivalent to just:
            > y
            >
            > So you effectively have
            > printf("%d ", array[y])[/color]

            Ok thanks, when I changed the way it was typed it worked much better..
            Strangly the array[x,y] came straight from the "Practical C" book.

            The new initialization is this:
            array[0] 0 10 20
            array[1] 1 11 21
            array[2] 2 12 22
            array[3] 3 13 23
            array[4] 4 14 24

            This seems like a much more plausible answer.. Although I am still a little
            confused as to how it arrives at the numbers in that order. Could someone
            explain it to me a little clearer? The book is not doing a very good job.

            Thanks


            Comment

            • Martin Ambuhl

              #7
              Re: Array Initialization

              Henry wrote:
              [color=blue]
              > I finally thought I had an understanding of multi dimensional arrays in C
              > when I get this:
              >
              > #include <stdio.h>
              >
              > #define max_x 3
              > #define max_y 5
              >
              > int array[max_x][max_y];
              >
              > main()[/color]

              For goodness' sake! Why would anyone use an implicit int as the return
              value for main, and then decorate printf calls with casts?
              [...]
              [color=blue]
              > (void)printf("a rray[%d] ", y);[/color]

              If you do this to satisfy some version of lint, get one that knows that not
              only is an explicit 'int' on main a good idea, but it is required by the
              current C standard.



              --
              Martin Ambuhl

              Comment

              • Irrwahn Grausewitz

                #8
                Re: Array Initialization

                "Henry" <henryhc@knolog y.net> wrote in
                <vlagssbqf5q9f3 @corp.supernews .com>:[color=blue]
                >I finally thought I had an understanding of multi dimensional arrays in C
                >when I get this:
                >
                >#include <stdio.h>
                >
                >#define max_x 3
                >#define max_y 5
                >
                >int array[max_x][max_y];
                >
                >main()[/color]
                int main(void)[color=blue]
                >{
                > int x,y;
                >
                > for(x = 0; x < max_x; x++)
                > for(y=0; y < max_y; y++)
                > array[x][y] = x * 10 + y;
                >
                > for(y = 0; y < max_y; y++) {
                > (void)printf("a rray[%d] ", y);[/color]
                Get rid of these (void) casts - you don't need them.
                [color=blue]
                > for(x = 0; x < max_x; x++)
                > (void)printf("% d ", array[x,y]);[/color]
                ^^^^^
                Shouldn't this read: array[x][y] ???
                [color=blue]
                > (void)printf("\ n");
                > }
                >
                > return 0;
                >}
                >
                >This program initializes to :
                >array[0] 56164 56164 56164
                >array[1] 56184 56184 56184
                >array[2] 56204 56204 56204
                >array[3] 56224 56224 56224
                >array[4] 56244 56244 56244[/color]

                Not at all - the initialization is just perfect.[color=blue]
                >
                >Could someone please help me figure out how this program arrives at such a
                >large number?
                >[/color]
                You print array[x,y], which is equivalent to array[y], which is
                in turn a pointer to an array of five ints, which you print using
                the %d format specifier - hence the 'strange' values.

                Irrwahn
                --
                Rain is just liquid sunshine.

                Comment

                • A. Bolmarcich

                  #9
                  Re: Array Initialization

                  In article <vlagssbqf5q9f3 @corp.supernews .com>, Henry wrote:[color=blue]
                  > I finally thought I had an understanding of multi dimensional arrays in C
                  > when I get this:
                  >
                  > #include <stdio.h>
                  >
                  > #define max_x 3
                  > #define max_y 5
                  >
                  > int array[max_x][max_y];
                  >
                  > main()
                  > {
                  > int x,y;
                  >
                  > for(x = 0; x < max_x; x++)
                  > for(y=0; y < max_y; y++)
                  > array[x][y] = x * 10 + y;
                  >
                  > for(y = 0; y < max_y; y++) {
                  > (void)printf("a rray[%d] ", y);
                  > for(x = 0; x < max_x; x++)
                  > (void)printf("% d ", array[x,y]);
                  > (void)printf("\ n");
                  > }
                  >
                  > return 0;
                  > }
                  >
                  > This program initializes to :
                  > array[0] 56164 56164 56164
                  > array[1] 56184 56184 56184
                  > array[2] 56204 56204 56204
                  > array[3] 56224 56224 56224
                  > array[4] 56244 56244 56244
                  >
                  > Could someone please help me figure out how this program arrives at such a
                  > large number?[/color]

                  The statement

                  (void)printf("% d ", array[x,y]);

                  prints the value of array[y] because the value of expression x,y is y.
                  You likely meant to write

                  (void)printf("% d ", array[x][y]);

                  Because the array is a two dimensional array, the value of array[y] is a
                  pointer to a one-dimensional array. The values being printing in each
                  row are &array[0][0], &array[1][0], &array[2][0], &array[3][0], and
                  &array[4][0].

                  Comment

                  • Irrwahn Grausewitz

                    #10
                    Re: Array Initialization

                    Irrwahn Grausewitz <irrwahn@freene t.de> wrote in
                    <uhialvkvu9u7i0 qvgbgss7j0d3t4d tgo0e@4ax.com>:

                    Arrrrrrrrrrrgh, I did it again, stupid me!
                    <snip>[color=blue]
                    >in turn a pointer to an array of five ints, which you print using[/color]
                    ^^^^^^^^^^^^^^^ ^
                    <delete this>
                    <snap>

                    --
                    Rain is just liquid sunshine.

                    Comment

                    • Irrwahn Grausewitz

                      #11
                      Re: Array Initialization

                      "Henry" <henryhc@knolog y.net> wrote in
                      <vlaig9e9eht590 @corp.supernews .com>:
                      <SNIP>[color=blue]
                      >Ok thanks, when I changed the way it was typed it worked much better..
                      >Strangly the array[x,y] came straight from the "Practical C" book.[/color]
                      If there are more flaws like this in the book, get a better one.
                      (Btw.: were the (void) casts for printf straight from that book, too?[color=blue]
                      >
                      >The new initialization is this:
                      >array[0] 0 10 20
                      >array[1] 1 11 21
                      >array[2] 2 12 22
                      >array[3] 3 13 23
                      >array[4] 4 14 24
                      >
                      >This seems like a much more plausible answer.. Although I am still a little
                      >confused as to how it arrives at the numbers in that order. Could someone
                      >explain it to me a little clearer? The book is not doing a very good job.
                      >[/color]
                      You initialize the array like that:

                      for(x = 0; x < max_x; x++)
                      for(y = 0; y < max_y; y++)
                      array[x][y] = x * 10 + y;

                      and print it out this way:

                      for(y = 0; y < max_y; y++)
                      {
                      printf("array[%d] ", y);
                      for(x = 0; x < max_x; x++)
                      printf("%d ", array[x,y]);
                      printf("\n");
                      }

                      and this is what you get:
                      x
                      0 1 2
                      y +---------
                      0| 0 10 20
                      1| 1 11 21
                      2| 2 12 22
                      3| 3 13 23
                      4| 4 14 24

                      Your confusion may rise from the fact that you arranged the loops
                      in different ways for the initialization and the output, but that
                      changes absolutely nothing - well, as long as you use x and y in
                      a consistent manner, which you perfectly did.

                      --
                      Rain is just liquid sunshine.

                      Comment

                      • Jeff

                        #12
                        Re: Array Initialization


                        "Brett Frankenberger" <rbf@panix.co m> wrote in message
                        news:bj3h6s$gi6 $1@reader2.pani x.com...[color=blue]
                        > In article <20030902214254 .22819.00000248 @mb-m23.aol.com>,
                        > Bigdakine <bigdakine@aol. comGetaGrip> wrote:[color=green]
                        > >
                        > >Heck, I'm interested to know how this even compiled.
                        > > (void)printf("% d ", array[x,y]);[/color]
                        >
                        > Why wouldn't it? 'x,y' is a perfectly valid expression (equivalent to
                        > 'y'), and array[y] doesn't violate any constraints (and would even be
                        > useful, defined behavior under some circumstances), so one wouldn't
                        > expect compilation to fail. Of course, array[y] is type (pointer to
                        > int), so attempting to print it with "%d" is undefined behavior.
                        >
                        > -- Brett[/color]

                        Yes, it can be compiled. The comma between a and b is "comma operator".
                        The expression "a,b" will return "b" silently. You can even write

                        array[ printf("Hello man") , y]


                        To OP :
                        It dosen't mean "array[x,y] " is the correct syntax for accessing
                        two-dimensional
                        array. You should write "array[x][y]".

                        --
                        Jeff




                        Comment

                        • Jeff

                          #13
                          Re: Array Initialization


                          "Henry" <henryhc@knolog y.net> wrote in message
                          news:vlaig9e9eh t590@corp.super news.com...[color=blue][color=green]
                          > > is a comma expression whose value is the value of the second
                          > > expression (in this case, y). The first expression (in this case, x)
                          > > would be evaluated for its side effects; but in this case, there are
                          > > none. So x,y is equivalent to just:
                          > > y
                          > >
                          > > So you effectively have
                          > > printf("%d ", array[y])[/color]
                          >
                          > Ok thanks, when I changed the way it was typed it worked much better..
                          > Strangly the array[x,y] came straight from the "Practical C" book.[/color]

                          Burn your book :-)

                          [snip]

                          --
                          Jeff


                          Comment

                          • Henry

                            #14
                            Re: Array Initialization

                            > > Ok thanks, when I changed the way it was typed it worked much better..[color=blue][color=green]
                            > > Strangly the array[x,y] came straight from the "Practical C" book.[/color]
                            >
                            > Burn your book :-)
                            >
                            > [snip]
                            >
                            > --
                            > Jeff[/color]

                            LOL

                            Well I started learning C using the K&R book but it assumed too much, so I
                            was told "Practical C" would be a much better book to go through because it
                            would be more in depth and would cover topics that K&R would seemingly skip
                            over. So far "PC" has been compeltly different.. using commands such as
                            scanf and fgets which K&R didn't even mention


                            Comment

                            • Henry

                              #15
                              Re: Array Initialization


                              "Irrwahn Grausewitz" <irrwahn@freene t.de> wrote in message
                              news:62jalvco4m t87i4994jbbam95 mi808h42a@4ax.c om...[color=blue]
                              > "Henry" <henryhc@knolog y.net> wrote in
                              > <vlaig9e9eht590 @corp.supernews .com>:
                              > <SNIP>[color=green]
                              > >Ok thanks, when I changed the way it was typed it worked much better..
                              > >Strangly the array[x,y] came straight from the "Practical C" book.[/color]
                              > If there are more flaws like this in the book, get a better one.
                              > (Btw.: were the (void) casts for printf straight from that book, too?[/color]

                              Yes... it seemed strange to me that they would expect a return on printf


                              Comment

                              Working...