运行此代码时,会显示分段错误。但是当地址(LessThan)countarray更改为地址< = countarray时,它可以工作。我只是想让它打印少一个阵列,但它不会让我。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,check,divisor,countarray,address;
int pn[100];
for (n=2;n<100;n++){
for (divisor=2;divisor<n;divisor++){
if ((n/divisor)*divisor==n) //if (n is not a prime number)
check++;
}
if (check==0){ //if its a prime number,
pn[countarray]=n;
countarray++;
}
check=0;
}
for (address=0;address<countarray;address++)
printf("address for %d is %d and ",pn[address],address);
return 0;
}
答案 0 :(得分:3)
条件address<countarray
没有问题,您应该初始化检查&amp; countarray 变量。
int n,check=0,divisor,countarray=0,address;