首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求帮助:指针的动态分配如何运行完老出现这个有关问题(不会插图)

2012-10-23 
求帮助:指针的动态分配怎么运行完老出现这个问题(不会插图)#includestdio.h#includestring.h#include

求帮助:指针的动态分配怎么运行完老出现这个问题(不会插图)
#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

探讨

引用:

int *q = (int *)malloc(j-2);这里j是零,-2变负值,程序应该抛badalloc的异常

badalloc是什么意思?

[解决办法]
用户至少输入两个非负整数才能保证j-2>0
探讨

引用:

int *q = (int *)malloc(j-2);这里j是零,-2变负值,程序应该抛badalloc的异常

badalloc是什么意思?

热点排行