C 语言入门级问题,指针问题

2015-08-17 05:49:16 +08:00
 qw7692336
#include <stdio.h>

void main(){
    int a = 5;
    int* b = &a;
    int* c = &b;
    printf("%d\n",&a==b);
    printf("%d\n",&b==c);
    printf("%d\n",a==*b);
    printf("%d\n",b==*c);
}

输出是
1
1
1
0

为何最后一个是0?

1818 次点击
所在节点    C
25 条回复
wlee1991
2015-08-17 11:15:51 +08:00
我就不信弄不粗来

` ` `
#include <stdio.h>

int main (int argc, const char * argv[]) {

int a = 5; //a is int
int * b = &a; //b is a pointer to int
int * * c = &b; //c is a pointer to int *
int * * * d = &c; //d is a pointer to int * *

printf ("a = %d, &a = %p\n", a, &a );
printf ("b = %p, &b = %p\n", b, &b );
printf ("c = %p, &c = %p\n", c, &c );
printf ("d = %p, &d = %p\n", d, &d );

printf ("*b = %d\n", *b );
printf ("**c = %d\n", **c );
printf ("***d = %d\n", ***d );

printf ("%d\n", &a == b );
printf ("%d\n", &b == c );
printf ("%d\n", &c == d );

printf ("%d\n", a == *b );
printf ("%d\n", b == *c );
printf ("%d\n", c == *d );

return 0;
}
` ` `
testlc
2015-08-17 11:19:53 +08:00
手动 markdown 好毅力 哈哈
qw7692336
2015-08-17 11:44:33 +08:00
@nozama
@weyou
改为
long* c = &b;
会输出 1 ;
果然是 64 位截断问题
Banio
2015-08-17 16:18:29 +08:00
In function 'main':
[Warning] initialization from incompatible pointer type [enabled by default]
[Warning] comparison of distinct pointer types lacks a cast [enabled by default]
[Warning] comparison between pointer and integer [enabled by default]

1
1
1
1
用的 dev cpp 5.1
oska874
2015-08-17 17:13:57 +08:00
32bit linux,64 bit cygwin,用 gcc 4.8+编译,都是编译有 warning ,但是结果都是 4 个 1 。

别用 vc 6 了,我们搞工控的都是 vs 2013 了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/213658

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX