Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Please find out what is wrong

Reply
Thread Tools

Please find out what is wrong

 
 
pankaj tiwary
Guest
Posts: n/a
 
      09-10-2004
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.
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      09-10-2004
(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
 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      09-10-2004
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
 
Reply With Quote
 
Dave
Guest
Posts: n/a
 
      09-10-2004


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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-10-2004
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;
}


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      09-10-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 3 02-14-2011 05:28 AM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 0 02-10-2011 05:18 PM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM
How do I find out what's going wrong? Stubbly Wubbly ASP .Net 2 06-29-2005 05:24 PM
Could you help me to find out what is wrong the following usage of function object PengYu.UT@gmail.com C++ 5 04-16-2005 03:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57