void inputArray(int* *pa, int *n)
{
do {
printf("Input the elements: ");
scanf_s("%d", n);
if (*n < 0)
{
printf("Error! Input again\n");
}
} while (*n < 0);
*pa = (int*)malloc(*n * sizeof(int));
for (int i = 0; i < *n; i++)
{
printf("Elements a[%d]: ", i);
scanf_s("%d", pa + i);
}
}
void outputArray(int* *pa, int n)
{
printf("\nContent of the array\n");
for (int i = 0; i < n; i++)
{
printf("a[%d] = %d\n", i, *(pa + i));
}
}
int main()
{
int *A;
int n;
inputArray(&A, &n);
outputArray(&A, n);
free(A);
_getch();
return 0;
}
当程序显示数组时,我收到错误“Exception returned at 0x00AD372D (ucrtbased.dll)” 我尝试了很多次来修复该错误,但当程序显示数组的输出时,它仍然显示该错误,请给我一些建议。感谢您的阅读。
最佳答案
调试器将向您显示问题所在。
您将指针的地址传递给 inputArray
并将其作为 int **
: 还好
您用 *pa = (int*)malloc(*n * sizeof(int));
分配数组: 差不多就好了。所有间接级别都是正确的,但是您不应该在 C 中强制转换 malloc(应该是: *pa = malloc(*n * sizeof(int));
)
但是scanf_s("%d", pa + i);
完全错误。 pa
是一个指针,包含已分配数组的地址,因此该数组位于 *pa
不在pa
。你应该写scanf_s("%d", *pa + i);
对于outputArray
,您没有理由将指针传递给数组。但如果这样做,值将是 (*pa)[i]
,不在 *(pa + i)
与 pa[i]
相同,所以你应该使用:printf("a[%d] = %d\n", i, (*pa)[i]);
.
但正确的方法是:
void outputArray(int *pa, int n)
{
printf("\nContent of the array\n");
for (int i = 0; i < n; i++)
{
printf("a[%d] = %d\n", i, pa[i]);
}
}
...
outputArray(A, n);
关于c - 读取位置 0xFFFFFFFE 时发生访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42693049/