指针的动态分配怎么运行后老出现这个问题

来源:互联网 发布:2017中日贸易数据 编辑:程序博客网 时间:2024/04/29 23:18

代码:

#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;
}




}

原创粉丝点击