Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   strange program- shows not output (http://www.go4expert.com/forums/strange-program-output-t22698/)

k0der 13Jul2010 13:22

strange program- shows not output
 
Hi,

the following code compiles well but its nt showing output. Please specify what may be the error..


Code:

#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;
  }

Thanks

Poonamol 13Jul2010 14:13

Re: strange program- shows not output
 
In the for loop, value of D must be initialized to 0 instead of -1.
for(d=0;........)
:happy:

k0der 13Jul2010 15:51

Re: strange program- shows not output
 
thats fine.. but what is going wrong when d=-1 ?

Thanks

virxen 14Jul2010 04:35

Re: strange program- shows not output
 
try this

Code:

#include<stdio.h>

  #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
  int array[] = {23,34,12,17,204,99,16};

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


xpi0t0s 14Jul2010 14:38

Re: strange program- shows not output
 
It goes wrong because TOTAL_ELEMENTS evaluates to an unsigned int. This also displays no output:
Code:

        int d;

        unsigned int t=TOTAL_ELEMENTS-2;
        for(d=-1;d <= t;d++)
                printf("%d\n",array[d+1]);

You compiler should give you a warning. I got (in Visual Studio 2008):
warning C4018: '<=' : signed/unsigned mismatch

The problem with comparing signed with unsigned is that the signed number is treated as unsigned. -1 is 0xFFFFFFFF, which as an unsigned int has the value over 4 billion, which is not less than or equal to TOTAL_ELEMENTS (7), so you get no output.

Quote:

In the for loop, value of D must be initialized to 0 instead of -1.
No, this is absolutely false. It makes no difference what the start and stop values of d are, as long as a suitable offset is added when d is used as the index of an array, IF the pointer used is at the start of the array. This works perfectly:
Code:

        int d, *arr=&array[3];
       
        for (d=-3; d<4; d++)
                printf("%d ",arr[d]);
        printf("\n");
        for (d=-3; d<4; d++)
                printf("%d ",array[d+3]);

Both loops are from -3 to +3 (7 elements). The first loop displays arr[-3] to arr[3], which corresponds to array[0] to array[6] because arr points at array[3]. The second loop displays array[-3+3] to array[3+3], i.e. array[0] to array[6].

k0der 15Jul2010 14:07

Re: strange program- shows not output
 
Thanks a ton xpi0t0s!! Its was a great info!!:happy:

k0der 15Jul2010 14:18

Re: strange program- shows not output
 
Code:

int main()
  {
      int* p;
      p = (int*)malloc(sizeof(int));
      *p = 10;
      return 0;
  }

Though this works fine on IA-32, but does not work on IA-64. I am not able to find the answer,why it is so.

Please tell me.

Thanks

xpi0t0s 15Jul2010 16:43

Re: strange program- shows not output
 
How exactly doesn't it work? There is no output. What are you expecting, and what did you observe that didn't match your expectations? What OS are you using - IA32/IA6 is a processor architecture, and runs several different OS's.

jimblumberg 16Jul2010 20:51

Re: strange program- shows not output
 
Could it be that sizeof(int) != sizeof(int *) in 64 bit?

xpi0t0s 17Jul2010 13:58

Re: strange program- shows not output
 
Why would that make any difference? p is a pointer to int. malloc allocates space for an int and returns the pointer, cast to an int* to avoid compiler warnings, into p. The next line then dereferences p and plonks 10 into it. Should work fine, i.e. set *p, which is an int, to 10.

In fact the code should be identical to
Code:

int p;
p=10;
malloc(sizeof(int)); // to duplicate the memory leak in the original code



All times are GMT +5.5. The time now is 18:22.