array values in for loop

• 11-03-2012
austinweisgrau
array values in for loop
Code:

```int primeGen(a1){   int b1;   int b2;   b2=0;   int b4;   int *b5;   int b6;   b5 = (int *)calloc(a1*2,sizeof(int));   b5[0]=1;   b5[1]=2;   b5[3]=2;   b5[4]=3;   for(b1=5; b2 < a1; b1++){     if (checkPrime(b1)) {         /* What I'm doing is ordering the array b5         such that entry i*2 is the ith index,         and that i*2+1 is the ith prime */       b5[5+b2*2]=3+b2; //These seem to be working fine       b5[6+b2*2]=b1;       b2++;     }   }   for(b6=0;b6<a1;b6++){     printf("%d %d \n",b5[b6],b5[b6+1]); //But down here the data's all messed up   }   return 0; }```
I'm not sure what's going wrong. The data is correct when it's assigned, but after the for loop I try and get the values from the array and they seem totally random. What's going on?
Thanks
• 11-03-2012
qny
Quote:

Code:

```  for(b6=0;b6<a1;b6++){     printf("%d %d \n",b5[b6],b5[b6+1]); //But down here the data's all messed up   }```

You are printing the elements twice. For example element at index 3. you print it in second position when b6 is 2; and on the next line, when b6 is 3, you print it again in first position.