Array Initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joona I Palaste

    #16
    Re: Array Initialization

    Jeff <nothing@notexi st.com> scribbled the following:[color=blue]
    > "Brett Frankenberger" <rbf@panix.co m> wrote in message
    > news:bj3h6s$gi6 $1@reader2.pani x.com...[color=green]
    >> In article <20030902214254 .22819.00000248 @mb-m23.aol.com>,
    >> Bigdakine <bigdakine@aol. comGetaGrip> wrote:[color=darkred]
    >> >
    >> >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.[/color][/color]
    [color=blue]
    > 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[/color]
    [color=blue]
    > array[ printf("Hello man") , y][/color]
    [color=blue]
    > To OP :
    > It dosen't mean "array[x,y] " is the correct syntax for accessing
    > two-dimensional
    > array. You should write "array[x][y]".[/color]

    However, due to C's wacky ways of working, the following code is
    perfectly legal:

    #include <stdio.h>
    int main(void) {
    int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    printf("%d\n", array[printf("Hello man\n")]);
    return 0;
    }

    This should print out:
    Hello man
    11

    Because "Hello man\n" is 10 printable characters (and a '\0'), and
    array[10] is 11.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "And according to Occam's Toothbrush, we only need to optimise the most frequent
    instructions."
    - Teemu Kerola

    Comment

    • Martien Verbruggen

      #17
      Re: Array Initialization

      On Tue, 2 Sep 2003 23:10:21 -0400,
      Henry <henryhc@knolog y.net> wrote:[color=blue]
      >
      > "Irrwahn Grausewitz" <irrwahn@freene t.de> wrote in message
      > news:62jalvco4m t87i4994jbbam95 mi808h42a@4ax.c om...[color=green]
      >> "Henry" <henryhc@knolog y.net> wrote in
      >> <vlaig9e9eht590 @corp.supernews .com>:
      >> <SNIP>[color=darkred]
      >> >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[/color]

      That's not strange, since printf() does return a value. Explicitly
      casting it away like that, however, isn't really necessary. It used to
      be done a lot to shut up warnings from lint about discarded return
      values, but it isn't necessary to do.

      Martien
      --
      |
      Martien Verbruggen | The problem with sharks is that they are too
      | large to get to the shallow end of the gene
      | pool. -- Scott R. Godin

      Comment

      • Jens.Toerring@physik.fu-berlin.de

        #18
        Re: Array Initialization

        Henry <henryhc@knolog y.net> wrote:[color=blue][color=green][color=darkred]
        >> > 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 :-)[/color][/color]
        [color=blue]
        > 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[/color]

        You better have a another look at your K&R2 (I hope you have the second
        edition), and go to section 7.4 (completely devoted to scanf()) and 7.7
        (discusses fgets() and fputs()). K&R probably doesn't introduce these
        functions earlier because you need to have understood about pointers
        and arrays before you can use them. I guess that a lot of the questions
        you see asked in clc why stuff like

        char *my_string;
        double my_float;
        scanf( "%f %s", my_float, my_string );

        does not work may be due to books introducing scanf() too early...

        Regards, Jens
        --
        _ _____ _____
        | ||_ _||_ _| Jens.Toerring@p hysik.fu-berlin.de
        _ | | | | | |
        | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
        \___/ens|_|homs|_|oe rring

        Comment

        • Irrwahn Grausewitz

          #19
          Re: Array Initialization

          "Henry" <henryhc@knolog y.net> wrote in
          <vlamrim0cu7ec3 @corp.supernews .com>:
          [color=blue][color=green][color=darkred]
          >> > 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[/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[/color]

          Huh? You must own a very rare preliminary version of K&R! :)

          In my copy of K&R2 fgets and (f)scanf are used frequently in
          the examples. scanf has a section dedicated to it as well:
          Chapter 7.4: Formatted Input - Scanf
          And in 7.7 the source code for a possible fgets/fputs
          implementation is given...


          --
          No sig today.

          Comment

          • Stuart

            #20
            Re: Array Initialization

            rbf@panix.com (Brett Frankenberger) wrote in message news:<bj3h6s$gi 6$1@reader2.pan ix.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]

            I forgot about the "," operator.

            Thanx. Be that as it may, if the intent is to print an element of a
            2-d array, it should be written as [x][y]

            Stuart

            Comment

            Working...