0

I programmed the next code in c.

What have i done wrong:

int *ip;
int i;
ip = (int*) malloc(5*sizeof(int));

for(i=0;i<5;i++){
  ip[i]=i;
}

When I run over the code with the debugger it's like I'm not iterating over IP.

Could someone help?

2 Answers2

0

its works

#include <stdio.h>
#include <stdlib.h>
int main()
{
     int *ip;
     int i;
     ip = (int *) malloc(5*sizeof(int));

     for(i=0;i<5;i++){
         ip[i]=i;
     }

     for(i=0;i<5;i++){
          printf(" %d ",ip[i]);
      }
      free(ip);
      return 0;
}

out put 0 1 2 3 4

Nooh
  • 1,548
  • 13
  • 21
0

I assume the problem as follows: when you walk though the program step-by-step using the debugger, you cannot see it running the loop 5 times.

The reason may lie in the optimizations performed by the C compiler, especially loop unrolling.

If you use gcc, try the option -O0 to avoid optimizations. This may help debugging.