Please find out what is wrong

P

pankaj tiwary

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.
 
R

Richard Bos

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
 
E

E. Robert Tisdale

pankaj said:
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
 
D

Dave

pankaj said:
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.
 
M

Martin Ambuhl

pankaj said:
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;
}
 
P

pete

pankaj said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top