Please find out what is wrong

Discussion in 'C Programming' started by pankaj tiwary, Sep 10, 2004.

  1. Hello experts, please consider the code fragment:-

    #include<stdio.h>

    #define TOTAL_ELEMENTS (sizeof(array)/sizeof(array[0]))

    int array[] = {23,34,12,17,204,99,16};

    int main()
    {
    int d;
    for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
    printf("%d\n",array[d+1]);
    return 0;
    }

    It doesn't print the elements of the array?

    Thanks in advance.
     
    pankaj tiwary, Sep 10, 2004
    #1
    1. Advertising

  2. pankaj tiwary

    Richard Bos Guest

    (pankaj tiwary) wrote:

    > Hello experts, please consider the code fragment:-
    >
    > #include<stdio.h>
    >
    > #define TOTAL_ELEMENTS (sizeof(array)/sizeof(array[0]))
    >
    > int array[] = {23,34,12,17,204,99,16};
    >
    > int main()
    > {
    > int d;
    > for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
    > printf("%d\n",array[d+1]);
    > return 0;
    > }
    >
    > It doesn't print the elements of the array?


    Fiddle with it. Change little things to see if you can get the problem
    to go away. Ponder how this loop is different from normal loops, and
    what influence this could have on the result.
    If you're still stuck, here are a couple of successive hints:





    Hint 0: What actually happens in the comparison in your for statement?





    Hint 1: sizeof gives a size_t. What is a size_t?





    Hint 2: size_t is an unsigned type.





    Hint 3: What happens when you have a (possibly large) unsigned type and
    a signed int in a comparison?





    Hint 4: For the comparison d <= (TOTAL_ELEMENTS-2), d is converted to
    an unsigned type compatible with size_t, not v.v. What is the result?





    Hint 5: d starts out negative. What happens when you convert a small
    negative int to a large unsigned type?





    Hint 6: What could you to to ensure that the comparison is done using
    (signed) ints?





    No more hints. The next explanation is explicit.




    For the comparison in the for loop, d is converted to a size_t, and then
    both unsigned values are compared. This would not usually be a problem,
    _except_ that d starts out as -1, which is converted to SIZE_MAX-2 - a
    very _large_ positive number, not a negative one. So on the first tour
    through the for loop, the comparison fails.
    To get around this, you could, for example, cast TOTAL_ELEMENTS or
    (TOTAL_ELEMENTS-1) to int. This makes the comparison one between two
    signed integers; no more conversion is needed; and the comparison works
    as you'd expect it to.

    Richard
     
    Richard Bos, Sep 10, 2004
    #2
    1. Advertising

  3. pankaj tiwary wrote:

    > Hello experts, please consider the code fragment:-
    >
    > #include<stdio.h>
    >
    > #define TOTAL_ELEMENTS (sizeof(array)/sizeof(array[0]))
    >
    > int array[] = {23, 34, 12, 17, 204, 99, 16};
    >
    > int main() {
    > int d;
    > for(d = -1; d <= (TOTAL_ELEMENTS - 2); d++)
    > printf("%d\n", array[d+1]);
    > return 0;
    > }
    >
    > It doesn't print the elements of the array?


    > cat main.c

    #include<stdio.h>

    #define TOTAL_ELEMENTS ((int)(sizeof(array)/sizeof(array[0])))

    int array[] = {23, 34, 12, 17, 204, 99, 16};

    int main(int argc, char* argv[]) {
    for (int d = -1; d <= (TOTAL_ELEMENTS - 2); d++)
    printf("%d\n", array[d+1]);
    return 0;
    }

    > gcc -Wall -std=c99 -pedantic -o main main.c
    > ./main

    23
    34
    12
    17
    204
    99
    16
     
    E. Robert Tisdale, Sep 10, 2004
    #3
  4. pankaj tiwary

    Dave Guest

    pankaj tiwary wrote:
    > Hello experts, please consider the code fragment:-
    >
    > #include<stdio.h>
    >
    > #define TOTAL_ELEMENTS (sizeof(array)/sizeof(array[0]))
    >
    > int array[] = {23,34,12,17,204,99,16};
    >
    > int main()
    > {
    > int d;
    > for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
    > printf("%d\n",array[d+1]);
    > return 0;
    > }
    >
    > It doesn't print the elements of the array?
    >
    > Thanks in advance.


    Replacing (TOTAL_ELEMENTS-2) with 5 works.
    Replacing -1 with 0 works (except it doesn't print the first element).
    Casting (TOTAL_ELEMENTS-2) to int works.

    Perhaps there's something about sizeof that makes comparison with -1 not
    work as you expected?

    Major clue: Is 4294967295 greater or less than 5?

    Dave.
     
    Dave, Sep 10, 2004
    #4
  5. pankaj tiwary wrote:
    > Hello experts, please consider the code fragment:-
    >
    > #include<stdio.h>
    >
    > #define TOTAL_ELEMENTS (sizeof(array)/sizeof(array[0]))
    >
    > int array[] = {23,34,12,17,204,99,16};
    >
    > int main()
    > {
    > int d;
    > for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
    > printf("%d\n",array[d+1]);
    > return 0;
    > }
    >
    > It doesn't print the elements of the array?


    See if this does. If so, consider what the difference might be.

    #include<stdio.h>

    #define TOTAL_ELEMENTS (int)(sizeof(array)/sizeof(array[0]))

    int array[] = { 23, 34, 12, 17, 204, 99, 16 };

    int main()
    {
    int d;
    for (d = -1; d <= (TOTAL_ELEMENTS - 2); d++)
    printf("%d\n", array[d + 1]);
    return 0;
    }
     
    Martin Ambuhl, Sep 10, 2004
    #5
  6. pankaj tiwary

    pete Guest

    pankaj tiwary wrote:
    >
    > Hello experts, please consider the code fragment:-
    >
    > #include<stdio.h>
    >
    > #define TOTAL_ELEMENTS (sizeof(array)/sizeof(array[0]))
    >
    > int array[] = {23,34,12,17,204,99,16};
    >
    > int main()
    > {
    > int d;
    > for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
    > printf("%d\n",array[d+1]);
    > return 0;
    > }
    >
    > It doesn't print the elements of the array?


    #include<stdio.h>

    #define TOTAL_ELEMENTS (sizeof array / sizeof array[0])

    int array[] = {23,34,12,17,204,99,16};

    int main(void)
    {
    int d;

    for(d = 0; d != TOTAL_ELEMENTS; d++) {
    printf("%d\n",array[d]);
    }
    return 0;
    }

    --
    pete
     
    pete, Sep 10, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stubbly Wubbly

    How do I find out what's going wrong?

    Stubbly Wubbly, Jun 29, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    496
    Stubbly Wubbly
    Jun 29, 2005
  2. KK
    Replies:
    2
    Views:
    596
    Big Brian
    Oct 14, 2003
  3. Replies:
    5
    Views:
    320
    John Carson
    Apr 16, 2005
  4. Wybo Dekker
    Replies:
    1
    Views:
    366
    Yukihiro Matsumoto
    Nov 15, 2005
  5. _Raven
    Replies:
    4
    Views:
    122
    _Raven
    Aug 11, 2006
Loading...

Share This Page