求帮助:指针的动态分配怎么运行完老出现这个问题(不会插图)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int a[50]={0};
int j=0,i=0,n=0,m=0;
printf("please input the mark(student number between 0-50)and end with a number litter than 0: \n");
for(j=0;j<50;j++)
{
scanf("%d",&a[j]);
if(a[j]<0)
{
break;
}
}
int *q = (int *)malloc(j-2);
q=&a[0];
while(m==0)
{
printf("please input the student you want to search\n");
scanf("%d",&i);
printf("%d\n",*(q+i-1));
printf("if you want to continue plesae input 0,enter any to esc:\n");
scanf("%d",&m);
}
if(q!=NULL)
{
free(q);
q=NULL;
}
}
[解决办法]
用户至少输入两个才能保证非负整数才能保证j-2>0