在以下code运行时错误 [英] Runtime error in the following code
问题描述
以下code,据我要成功运行,但在runtime.I失败,没有得到的原因:
The following code,according to me should run successfully,but fails at runtime.I don't get the reason:
void main()
{
int arr[5][3]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int *m=arr[0];
int **p=&m;
p=p+1;
printf("%d",**p);
}
A.EXE已经停止在gcc编译运行工作,Windows 7的64位
a.exe has stopped working at runtime in gcc compiler,windows 7 64 bit
推荐答案
数组的数组和指针的指针是完全不同的,不能互换使用。
An array of arrays and a pointer to a pointer is quite different, and can't be used interchangeably.
例如,如果你看看你的阵列改编
它看起来像这样在内存
For example, if you look at your array arr
it looks like this in memory
+-----------+-----------+-----------+-----------+-----+-----------+
| arr[0][0] | arr[0][1] | arr[0][2] | arr[1][0] | ... | arr[4][2] |
+-----------+-----------+-----------+-----------+-----+-----------+
当你有指针到指针 P
节目真的不知道,它指向数组的数组,相反,它会被视为指针数组,它看起来像这样的记忆:
When you have the pointer-to-pointer p
the program don't really knows that it points to an array of arrays, instead it's treated as an array of pointers, which looks like this in memory:
+------+------+------+-----+
| p[0] | p[1] | p[2] | ... |
+------+------+------+-----+
| | |
| | v
| | something
| v
| something
v
something
所以,当你做 P + 1
你到 P [1]
这显然是不一样的改编[1]
。
So when you do p + 1
you get to p[1]
which is clearly not the same as arr[1]
.
这篇关于在以下code运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!