-
运行此代码时,它会显示分段错误。但是当地址(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; }
条件address<countarray
没有问题,你应该初始化check&countarray变量。
int n,check=0,divisor,countarray=0,address;