Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: loop indices in C array

  1. #1
    Join Date
    Aug 2010
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Red face loop indices in C array

    i found this code from "O'Reilly Practical C"
    But i dont understand these things like "0 * 10 + 0" in front of array.

    ************************************************** *******
    #include <stdio.h>
    int array[3][2]; /* Array of numbers */
    int main()
    {
    int x,y; /* Loop indicies */
    array[0][0] = 0 * 10 + 0;
    array[0][1] = 0 * 10 + 1;
    array[1][0] = 1 * 10 + 0;
    array[1][1] = 1 * 10 + 1;
    array[2][0] = 2 * 10 + 0;
    array[2][1] = 2 * 10 + 1;
    printf("array[%d] ", 0);
    printf("%d ", array[0,0]);
    printf("%d ", array[0,1]);
    printf("\n");
    printf("array[%d] ", 1);
    printf("%d ", array[1,0]);
    printf("%d ", array[1,1]);
    printf("\n");
    printf("array[%d] ", 2);
    printf("%d ", array[2,0]);
    printf("%d ", array[2,1]);
    printf("\n");
    return (0);
    }
    ************************************************** *************
    OUTPUT of Program:-
    array[0] 134520864 134520872
    array[1] 134520864 134520872
    array[2] 134520864 134520872
    ************************************************** *************
    ofcourse it prints out memory of row and column but how? Are numbers being used as pointers?

  2. #2
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: loop indices in C array

    Those are the calculations for the offset of a given element stored in row-major order in an array.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  3. #3
    Join Date
    Nov 2009
    Beans
    1,081

    Re: loop indices in C array

    Screams 'interpret this as homework', not 'this code is an example'. The x and y aren't used; neither are the 0 * 10 + 1. Those are all red herrings.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
            int foo;
            foo = 4,1000;
            printf("%d\n", foo);
            return 0;
    }
    Compile and run. Then apply what you realize to the example.

  4. #4
    Join Date
    Dec 2006
    Beans
    256

    Re: loop indices in C array

    Quote Originally Posted by Zacinfinite View Post
    [snip]
    printf("array[%d] ", 2);
    printf("%d ", array[2,0]);
    printf("%d ", array[2,1]);
    printf("\n");
    return (0);
    }
    ************************************************** *************
    OUTPUT of Program:-
    array[0] 134520864 134520872
    array[1] 134520864 134520872
    array[2] 134520864 134520872
    ************************************************** *************
    ofcourse it prints out memory of row and column but how? Are numbers being used as pointers?
    The sample code is (intentionally?) broken. You can not access multi-dimension arrays with commas separating the indices. An argument such as 'array[0,1]' actually evaluates to address of the second row in the array ('array[1]'), not the element in second column of the first row. (In C, expressions separated by commas are evaluated in sequence.)

    If you wanted to retrieve the second element in the first row, proper C syntax would 'array[0][1]'.

    My guess is that the book is either intentionally presenting you with bad code (in hopes that you will learn from discovering it), or that the book is actually not for C but for C++ (C++ permits comma-separated multi-dimension array indexing).
    Last edited by saulgoode; August 21st, 2010 at 06:34 AM.
    "We visited sixty-six islands and landed eighty-one times, wading, swimming (to shore). Most of the people were friendly and delightful; only two arrows shot at us, and only one went near -- So much for savages!" - J.C. Patterson

  5. #5
    Join Date
    Jul 2006
    Location
    somewhere :)
    Beans
    535
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: loop indices in C array

    Quote Originally Posted by saulgoode View Post
    The sample code is (intentionally?) broken. You can not access multi-dimension arrays with commas separating the indices. An argument such as 'array[0,1]' actually evaluates to address of the second row in the array ('array[1]'), not the element in second column of the first row. (In C, expressions separated by commas are evaluated in sequence.)

    If you wanted to retrieve the second element in the first row, proper C syntax would 'array[0][1]'.

    My guess is that the book is either intentionally presenting you with bad code (in hopes that you will learn from discovering it), or that the book is actually not for C but for C++ (C++ permits comma-separated multi-dimension array indexing).
    not meaning to be picky and at the risk of confusing the op, but afaik in c the order of execution of statements separated by commas is not defined.


    ----edit----

    oh, hang on, it appears i'm wrong. as an operator, a list of statements separated by commas is evaluated from left to right. as a separator its order is undefined. now you just need a way to tell these two uses apart ...
    Last edited by howlingmadhowie; August 21st, 2010 at 08:48 AM. Reason: my mistake :)
    there are 10 types of people in the world: those that understand binary and i don't know who the other F are.

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: loop indices in C array

    Quote Originally Posted by howlingmadhowie View Post
    not meaning to be picky and at the risk of confusing the op, but afaik in c the order of execution of statements separated by commas is not defined.


    ----edit----

    oh, hang on, it appears i'm wrong. as an operator, a list of statements separated by commas is evaluated from left to right. as a separator its order is undefined. now you just need a way to tell these two uses apart ...
    If the -Wall compiler option is used, a warning similar to the following is generated when one attempts to index an array with comma-separated values.
    Code:
    warning: left-hand operand of comma expression has no effect
    Attempting to print the value of a two-dimensional int array, using the format of %d, and with the comma-separated indices also yields the following warning:
    Code:
    warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    The OP obviously did not use the -Wall option when compiling, or if he did, then he totally neglected the warning messages.

  7. #7
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: loop indices in C array

    Quote Originally Posted by saulgoode View Post
    ... C++ permits comma-separated multi-dimension array indexing.
    Show me an example, because otherwise this is news to me.

  8. #8
    Join Date
    Feb 2009
    Beans
    1,469

    Re: loop indices in C array

    Quote Originally Posted by Some Penguin View Post
    Screams 'interpret this as homework', not 'this code is an example'. The x and y aren't used; neither are the 0 * 10 + 1. Those are all red herrings.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
            int foo;
            foo = 4,1000;
            printf("%d\n", foo);
            return 0;
    }
    Compile and run. Then apply what you realize to the example.
    I'd like to note that assignment binds more tightly than comma. Your example has a different output than your use of whitespace might imply.

  9. #9
    Join Date
    Dec 2006
    Beans
    256

    Re: loop indices in C array

    Quote Originally Posted by dwhitney67 View Post
    C++ permits comma-separated multi-dimension array indexing.
    Show me an example, because otherwise this is news to me.
    Your objection is justified. C++ does not support comma-separated indices, though Microsoft's "C++/CLI" dialect apparently does.
    "We visited sixty-six islands and landed eighty-one times, wading, swimming (to shore). Most of the people were friendly and delightful; only two arrows shot at us, and only one went near -- So much for savages!" - J.C. Patterson

  10. #10
    Join Date
    Aug 2010
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: loop indices in C array

    I guess it can be best understood no better than by experimentation, compiling the code, editing, and recompiling to see the output.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •