これは、ユーザーが 2 つの整数a
とを入力する C クラスの演習です。b
と の間のすべての整数を含む配列を返す関数を作成する必要がありa
ますb
。
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int* arrayfromatob(int a,int b,int *p)
{
int i;
for(i=0;i<=b-a+1;i++)
p[i]=a+i;
return p;
}
main()
{
int a,b,*p,i,temp;
puts("Give two integers:");
scanf("%d %d",&a,&b);
if(b<a)
{
temp=a;
a=b;
b=temp;
}
p=(int*)calloc(b-a+1,sizeof(int));
if(p==NULL)
{
puts("Could not allocate memory");
exit(1);
}
p=arrayfromatob(a,b,p);
for(i=0;i<b-a+1;i++)
printf("Number %d: %d\n",i+1,p[i]);
free(p);
system("pause");
}
このコードがクラッシュするのはなぜですか? ( free(p); に関係していると思いますが、よくわかりません...)