为什么程序总是崩溃?

// 素数求和问题.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
int tolsu(int*p,int n);
int _tmain(int argc, _TCHAR* argv[])
{
int n;
int num;
char bol;
int s;
printf("测试样本次数:");
scanf("%d",&n);
while(n--)
{
printf("样本个数:");
scanf("%d",&num);
//getchar();
int*p;
p=(int*)malloc(num*sizeof(int));
for(int i=0;i<num;i++)
scanf("%d",p[i]);
for(int i=0;i<num;i++)
printf("%d ",p[i]);
printf("请确认输入:Y/N");
scanf("%c",&bol);
if (bol=='Y')
{
s=tolsu(p,num);
printf("%d\n",s);
}
else
{
printf("请重新输入:");
break;
}
free(p);
}
return 0;
}
int tolsu(int*p,int n)
{
int i=0;
int tol=0;
int c=0;
while(i<n)
{
for(int s=1;s<p[i];s++)
{
if (p[i]%s==0)
{
c++;
if (c<=2)
tol+=p[i];
continue;
}
else
continue;
}
i++;
}
return tol;
}

for(int s=1;s<p[i];s++)
过多的数组直接访问,没有进行有效性判断。如if(i<0),if(p[i] < 0).

索引取得的值不一定存在吧,如果超过,那就是非法值了

C语言崩溃的原因大多是因为内存超界。没太仔细看你的code,你可以在考虑考虑问题出现在哪。哪的读写超界。或是指针用错了。