急!指针问题

cityyokel 2002-09-10 05:04:16
int main()
{
int *arrInt1=new int(10);
int *arrInt2=new int(10);
int *arrInt3=new int(10);
int *arrInt4=new int(10);
for(int i=0;i<10;i++)
{
arrInt1[i]=1;
arrInt2[i]=5;
arrInt3[i]=10;
arrInt4[i]=15;
}
int *arrInt[]={arrInt1,arrInt2,arrInt3,arrInt4};

for(int j=0;j<4;j++)
for(int n=0;n<10;n++)
cout<<arrInt[j][n]<<" ";
cout<<endl;

}

程序输出完毕,忽然弹出对话框:
Debug Assertion Failed!
......
忽略完了后,又正常。

这是为什么???

急!
...全文
49 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
benbebnmao 2002-09-10
  • 打赏
  • 举报
回复
为什么不声明成数组呢??
int arrInt1[10];
int arrInt2[10];
int arrInt3[10];
int arrInt4[10];
这样好一些
或者是:
int *arrInt1=(int*)malloc(sizeof(int)*10);
int *arrInt2=(int*)malloc(sizeof(int)*10);
int *arrInt3=(int*)malloc(sizeof(int)*10);
int *arrInt4=(int*)malloc(sizeof(int)*10)
ice_soft 2002-09-10
  • 打赏
  • 举报
回复
在C语言中:
int* array = (int*)malloc(sizeof(int)*10);
需要类型转化
而在C++中:
int* array = new int[10];
自动返回分配的类型,所以如果你声明为void*
那就需要类型转化,不同的编译器会有不同的处理方法。
tinytot 2002-09-10
  • 打赏
  • 举报
回复
我K!这个错了.这样写编译器只new出来一个整型,并赋值为10.
int *arrInt1=new int(10);
..............

这样写了
int *arrInt1=new int[10];
benbebnmao 2002-09-10
  • 打赏
  • 举报
回复
每次都这样么,你用的什么编译器?
你加了那几个头文件?
blue_coco 2002-09-10
  • 打赏
  • 举报
回复
int *arrInt1=new int[10];
int *arrInt2=new int[10];
int *arrInt3=new int[10];
int *arrInt4=new int[10];
申明错了。

记得delete
cityyokel 2002-09-10
  • 打赏
  • 举报
回复
int 应该是void.
笔误。

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧