printf("%d",*(& ptr2 + 2)) [英] printf("%d",*(&ptr2 + 2))
问题描述
亲爱的,
为什么在以下计划中
#include< stdio.h>
#include< stdlib.h>
int main(无效)
{
int x [] = {99,2,3,4,5};
int * ptr,** ptr2;
ptr = x;
ptr2 =& ptr;
printf("%d",*(& ptr2 + 2));
返回EXIT_SUCCESS;
}
printf("%d",*(& ptr2 + 2));正在打印数组的第一个元素
x?任何人都可以解释一下吗?
Dear all,
why in the following program
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int x[] = {99,2,3,4,5};
int *ptr,**ptr2;
ptr = x;
ptr2 = &ptr;
printf("%d",*(&ptr2 + 2));
return EXIT_SUCCESS;
}
printf("%d",*(&ptr2 + 2)); is printing the first element of the array
x ? can anybody explain ?
推荐答案
sophia写道:
sophia wrote:
亲爱的,
为什么在以下计划中
#include< stdio.h>
#include< stdlib.h>
int main(无效)
{
int x [] = {99,2,3,4, 5};
int * ptr,** ptr2;
ptr = x;
ptr2 =& ptr;
printf("%d",*(& ptr2 + 2));
返回EXIT_SUCCESS;
}
printf("%d",*(& ptr2 + 2));正在打印数组的第一个元素
x?谁能解释一下?
Dear all,
why in the following program
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int x[] = {99,2,3,4,5};
int *ptr,**ptr2;
ptr = x;
ptr2 = &ptr;
printf("%d",*(&ptr2 + 2));
return EXIT_SUCCESS;
}
printf("%d",*(&ptr2 + 2)); is printing the first element of the array
x ? can anybody explain ?
人们在哪里找到这些人为的例子?
答案是运气。我只是发生了'变量的方式
在内存中布局。
尝试构建64位然后看看你得到了什么。 />
-
Ian Collins。
Where do people find these contrived examples?
The answer is luck. I just happens that''s the way the variables are
laid out in memory.
Try building 64 bit and see what you get then.
--
Ian Collins.
4月25日,12:56 * pm,Ian Collins < ian-n ... @ hotmail.comwrote:
On Apr 25, 12:56*pm, Ian Collins <ian-n...@hotmail.comwrote:
sophia写道:
sophia wrote:
亲爱的,
Dear all,
为什么在以下程序中
why in the following program
* #include< stdio.h>
* #include< stdlib.h>
*#include<stdio.h>
*#include<stdlib.h>
* int main(void)
* {
*int main(void)
*{
* * int x [] = {99,2,3,4,5};
* * int * ptr,** ptr2;
* *int x[] = {99,2,3,4,5};
* *int *ptr,**ptr2;
* * ptr * = x;
* * ptr2 =& ptr;
* *ptr *= x;
* *ptr2 = &ptr;
* * printf("%d",*(& ptr2 + 2));
* *printf("%d",*(&ptr2 + 2));
* * return EXIT_SUCCESS;
*}
* *return EXIT_SUCCESS;
*}
printf("%d",*(& ptr2 + 2));正在打印数组的第一个元素
x?谁能解释一下?
printf("%d",*(&ptr2 + 2)); is printing the first element of the array
x ? can anybody explain ?
人们在哪里找到这些人为的例子?
答案是运气。 *我恰好发生了变量在内存中的布局方式。
Where do people find these contrived examples?
The answer is luck. *I just happens that''s the way the variables are laid out in memory.
不仅仅是运气,这是我的结论。
我试过改变x [0]的值不同的价值,但仍然是
它会正确打印o / p。
与编译器有什么关系?
我在lcc-win32编译器上检查了这个程序
There is something more than luck, that is my conclusion.
I tried changing the value of x[0] to different values but still
it will print the o/p correctly.
Has it any thing to do with the compiler ?
I checked this program on lcc-win32 compiler
尝试构建64位,看看你得到了什么。
Try building 64 bit and see what you get then.
我没有64位编译器
I don''t have a 64 bit compiler
sophia写道:
sophia wrote:
亲爱的,
为什么在以下计划中
#include< stdio.h>
# include< stdlib.h>
int main(无效)
{
int x [] = { 99,2,3,4,5};
int * ptr,** ptr2;
ptr = x;
ptr2 =& ptr;
printf("%d",*(和& ptr2 + 2));
r eturn EXIT_SUCCESS;
}
printf("%d",*(& ptr2 + 2));正在打印数组的第一个元素
x?谁能解释一下?
Dear all,
why in the following program
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int x[] = {99,2,3,4,5};
int *ptr,**ptr2;
ptr = x;
ptr2 = &ptr;
printf("%d",*(&ptr2 + 2));
return EXIT_SUCCESS;
}
printf("%d",*(&ptr2 + 2)); is printing the first element of the array
x ? can anybody explain ?
打印的内容完全是偶然的。
你的编译器没有产生警告吗?
什么是& ptr2的类型?这是int ***。什么是(& ptr2 +
2)的类型。同样如此。那么*(& ptr2 + 2)的类型是什么?它是int **。
%d期望什么类型。它需要一个int。
你想要的是:
printf("%d \ n",** ptr2);
What it prints is purely by chance.
Did your compiler not produce a warning?
What is the type of &ptr2? It''s int ***. What is the type of (&ptr2 +
2). Again the same. Then what''s the type of *(&ptr2 + 2)? It''s int **.
What type does %d expect. It expects an int.
What you want is:
printf("%d\n", **ptr2);
这篇关于printf("%d",*(& ptr2 + 2))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!