【发布时间】:2013-01-03 13:39:27
【问题描述】:
我的程序旨在在运行时为二维数组分配内存,然后将元素放入其中,然后显示它。我的 prog 抛出了一些异常,任何人都可以帮我识别它吗?
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int main()
{
int i,j,row, col;
int *ptr;
printf("enter size of row and col\n");
scanf("%d%d",&row,&col);
ptr = (int *)malloc(row*col*sizeof(int));
if(ptr==NULL)
{
printf("stderr, not able to allocate memory");
exit(1);
}
else
{
printf("enter the element");
for(i=0; i<row;i++)
for(j=0;j<col;j++)
{
scanf("%d",ptr[i+j]);
}
for(i=0; i<row;i++)
{
for(j=0;j<col;j++)
printf("%d ",ptr[i+j]);
printf("\n");
}
}
}
【问题讨论】:
-
scanf("%d",ptr[i+j]);这是做什么的? -
请发布您遇到的实际错误。
-
在调试器中运行它,看看哪里出错了。
-
我觉得你非常需要再次阅读一个像样的 C 教程的相关部分。
标签: c pointers memory-management multidimensional-array