解释为什么打印相同 [英] Explain why this prints the same

查看:66
本文介绍了解释为什么打印相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试在面向Linux的论坛上帮助某人。我已经把他原来的代码拿来了b $ b并清理了它,但我仍然想知道

的东西。这是代码:


#include< stdio.h>


int

main( void)

{

int a [] [3] = {{1,2,3},{4,5,6,},{7,8, 9}};

int * b =(int *)a;

int ** c =(int **)a;

int i = 0;

int num_elements = sizeof(a)/ sizeof(int);


for(i = 0; i< num_elements; ++ i)

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


返回0 ;

}


编译后我得到:

gcc -Wall -W -ansi -pedantic -g -O0 -c -o pointer_wonder.o

pointer_wonder.c

pointer_wonder.c:在函数`main''中:

pointer_wonder.c:13:警告:int格式,指针arg(arg 3)

gcc pointer_wonder.o -o pointer_wonder.exe


输出结果为:

* b = 1 * c = 1

* b = 2 * c = 2

* b = 3 * c = 3

* b = 4 * c = 4

* b = 5 * c = 5

* b = 6 * c = 6

* b = 7 * c = 7

* b = 8 * c = 8

* b = 9 * c = 9


为什么*相同* b和* c?


/ Eric

Hello, I''m trying to help someone on a linux-oriented forum. I''ve taken
his original code and cleaned it up, but I am still wondering about
something. Here''s the code:

#include <stdio.h>

int
main(void)
{
int a[][3]={ {1,2,3},{4,5,6,},{7,8,9} };
int *b = (int *)a;
int **c = (int **)a;
int i = 0;
int num_elements = sizeof(a) / sizeof(int);

for(i = 0; i< num_elements; ++i)
printf("*b = %d *c = %d\n", *b++, *c++);

return 0;
}

When compiled I get:
gcc -Wall -W -ansi -pedantic -g -O0 -c -o pointer_wonder.o
pointer_wonder.c
pointer_wonder.c: In function `main'':
pointer_wonder.c:13: warning: int format, pointer arg (arg 3)
gcc pointer_wonder.o -o pointer_wonder.exe

The output is:
*b = 1 *c = 1
*b = 2 *c = 2
*b = 3 *c = 3
*b = 4 *c = 4
*b = 5 *c = 5
*b = 6 *c = 6
*b = 7 *c = 7
*b = 8 *c = 8
*b = 9 *c = 9

Why is it same for *b and *c?

/ Eric

推荐答案



" Eric Lilja" < MI ******** @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...

"Eric Lilja" <mi********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
您好,我正在尝试在面向Linux的论坛上帮助某人。我已经拿走了他的原始代码并清理了它,但我仍然想知道
的东西。这是代码:

#include< stdio.h>

int
main(void)
{
int a [] [3] = {{1,2,3},{4,5,6,},{7,8,9}};
int * b =(int *)a;
int ** c =(int **)a;
int i = 0;
int num_elements = sizeof(a)/ sizeof(int);

for( i = 0; i< num_elements; ++ i)
printf(" * b =%d * c =%d \ n",* b ++,* c ++);

返回0;
}

编译时我得到:
gcc -Wall -W -ansi -pedantic -g -O0 -c -o pointer_wonder.o
pointer_wonder .c
pointer_wonder.c:在函数`main'':
pointer_wonder.c:13:警告:int格式,指针arg(arg 3)
gcc pointer_wonder.o -o pointer_wonder。 exe

输出为:
* b = 1 * c = 1
* b = 2 * c = 2
* b = 3 * c = 3
* b = 4 * c = 4
* b = 5 * c = 5
* b = 6 * c = 6
* b = 7 * c = 7 * b = 8 * c = 8
* b = 9 * c = 9
为什么* b和* c相同?
Hello, I''m trying to help someone on a linux-oriented forum. I''ve taken
his original code and cleaned it up, but I am still wondering about
something. Here''s the code:

#include <stdio.h>

int
main(void)
{
int a[][3]={ {1,2,3},{4,5,6,},{7,8,9} };
int *b = (int *)a;
int **c = (int **)a;
int i = 0;
int num_elements = sizeof(a) / sizeof(int);

for(i = 0; i< num_elements; ++i)
printf("*b = %d *c = %d\n", *b++, *c++);

return 0;
}

When compiled I get:
gcc -Wall -W -ansi -pedantic -g -O0 -c -o pointer_wonder.o
pointer_wonder.c
pointer_wonder.c: In function `main'':
pointer_wonder.c:13: warning: int format, pointer arg (arg 3)
gcc pointer_wonder.o -o pointer_wonder.exe

The output is:
*b = 1 *c = 1
*b = 2 *c = 2
*b = 3 *c = 3
*b = 4 *c = 4
*b = 5 *c = 5
*b = 6 *c = 6
*b = 7 *c = 7
*b = 8 *c = 8
*b = 9 *c = 9

Why is it same for *b and *c?




* b给出你支持$




* c给你一个int *


但是,每个的''值'取自a。


因此,printf真的会喜欢第三个arg的%p(至少)。



*b gives you back an int

whereas

*c gives you back an int *

However, the ''values'' of each are taken from a.

So, printf would really like a %p (at least) for the third arg.




" pemo" <我们*********** @ gmail.com>在消息中写道

news:dn ********** @ news.ox.ac.uk ...

"pemo" <us***********@gmail.com> wrote in message
news:dn**********@news.ox.ac.uk...

Eric Lilja的" < MI ******** @ gmail.com>在消息中写道
新闻:11 ********************** @ f14g2000cwb.googlegr oups.com ...

"Eric Lilja" <mi********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
您好,我正在尝试在面向Linux的论坛上帮助某人。我已经拿走了他的原始代码并清理了它,但我仍然想知道
的东西。这是代码:

#include< stdio.h>

int
main(void)
{
int a [] [3] = {{1,2,3},{4,5,6,},{7,8,9}};
int * b =(int *)a;
int ** c =(int **)a;
int i = 0;
int num_elements = sizeof(a)/ sizeof(int);

for( i = 0; i< num_elements; ++ i)
printf(" * b =%d * c =%d \ n",* b ++,* c ++);

返回0;
}

编译时我得到:
gcc -Wall -W -ansi -pedantic -g -O0 -c -o pointer_wonder.o
pointer_wonder .c
pointer_wonder.c:在函数`main'':
pointer_wonder.c:13:警告:int格式,指针arg(arg 3)
gcc pointer_wonder.o -o pointer_wonder。 exe

输出为:
* b = 1 * c = 1
* b = 2 * c = 2
* b = 3 * c = 3
* b = 4 * c = 4
* b = 5 * c = 5
* b = 6 * c = 6
* b = 7 * c = 7 * b = 8 * c = 8
* b = 9 * c = 9
为什么* b和* c相同?
Hello, I''m trying to help someone on a linux-oriented forum. I''ve taken
his original code and cleaned it up, but I am still wondering about
something. Here''s the code:

#include <stdio.h>

int
main(void)
{
int a[][3]={ {1,2,3},{4,5,6,},{7,8,9} };
int *b = (int *)a;
int **c = (int **)a;
int i = 0;
int num_elements = sizeof(a) / sizeof(int);

for(i = 0; i< num_elements; ++i)
printf("*b = %d *c = %d\n", *b++, *c++);

return 0;
}

When compiled I get:
gcc -Wall -W -ansi -pedantic -g -O0 -c -o pointer_wonder.o
pointer_wonder.c
pointer_wonder.c: In function `main'':
pointer_wonder.c:13: warning: int format, pointer arg (arg 3)
gcc pointer_wonder.o -o pointer_wonder.exe

The output is:
*b = 1 *c = 1
*b = 2 *c = 2
*b = 3 *c = 3
*b = 4 *c = 4
*b = 5 *c = 5
*b = 6 *c = 6
*b = 7 *c = 7
*b = 8 *c = 8
*b = 9 *c = 9

Why is it same for *b and *c?



* b给你b ack an int

然后

* c给你一个int *

然而,每个的''值'取自。

所以,printf真的很喜欢第三个arg的%p(至少)。



*b gives you back an int

whereas

*c gives you back an int *

However, the ''values'' of each are taken from a.

So, printf would really like a %p (at least) for the third arg.




所以,[我应该添加] * c'的值是1,2,3,...其中c是一个int *

(显然,你不想要解释那个!



So, [I should have added] *c''s value is 1, 2, 3, ... where c is an int *
(obviously, you wouldn''t want to deref that!


pemo写道:

" Eric Lilja" < MI ******** @ gmail.com>在消息中写道
新闻:11 ********************** @ f14g2000cwb.googlegr oups.com ...

"Eric Lilja" <mi********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
您好,我正在尝试在面向Linux的论坛上帮助某人。我已经拿走了他的原始代码并清理了它,但我仍然想知道
的东西。这是代码:

#include< stdio.h>

int
main(void)
{
int a [] [3] = {{1,2,3},{4,5,6,},{7,8,9}};
int * b =(int *)a;
int ** c =(int **)a;
int i = 0;
int num_elements = sizeof(a)/ sizeof(int);

for( i = 0; i< num_elements; ++ i)
printf(" * b =%d * c =%d \ n",* b ++,* c ++);

返回0;
}

编译时我得到:
gcc -Wall -W -ansi -pedantic -g -O0 -c -o pointer_wonder.o
pointer_wonder .c
pointer_wonder.c:在函数`main'':
pointer_wonder.c:13:警告:int格式,指针arg(arg 3)


请注意这里的警告。 (* c ++是一个int *类型。)

gcc pointer_wonder.o -o pointer_wonder.exe

输出结果为:
* b = 1 * c = 1
[...] * b = 9 * c = 9

为什么* b和* c相同?
* b给你一个int



* c给你一个int *

然而,''的值''每个都来自a。
Hello, I''m trying to help someone on a linux-oriented forum. I''ve taken
his original code and cleaned it up, but I am still wondering about
something. Here''s the code:

#include <stdio.h>

int
main(void)
{
int a[][3]={ {1,2,3},{4,5,6,},{7,8,9} };
int *b = (int *)a;
int **c = (int **)a;
int i = 0;
int num_elements = sizeof(a) / sizeof(int);

for(i = 0; i< num_elements; ++i)
printf("*b = %d *c = %d\n", *b++, *c++);

return 0;
}

When compiled I get:
gcc -Wall -W -ansi -pedantic -g -O0 -c -o pointer_wonder.o
pointer_wonder.c
pointer_wonder.c: In function `main'':
pointer_wonder.c:13: warning: int format, pointer arg (arg 3)
Note the warning here. ("*c++" is an "int*" type.)
gcc pointer_wonder.o -o pointer_wonder.exe

The output is:
*b = 1 *c = 1 [...] *b = 9 *c = 9

Why is it same for *b and *c?
*b gives you back an int

whereas

*c gives you back an int *

However, the ''values'' of each are taken from a.




恰好是sizeof(int)== sizeof(int *)。尝试使用

系统,这是不正确的,例如旧模型模式下的旧16位DOS编译器

。 (sizeof int == 2,sizeof int *是4.)


在这里的测试中,我得到:


* b = 1 * c = 1

* b = 2 * c = 3

* b = 3 * c = 5

* b = 4 * c = 7

* b = 5 * c = 9

* b = 6 * c = 2013

* b = 7 * c = 2013

* b = 8 * c = 9

* b = 9 * c = 2680

所以,printf真的很喜欢%p (至少)对于第三个arg。



And it happens to be that sizeof(int)==sizeof(int*). Try it on a
system where this is not true, such as an old 16-bit DOS compiler
in large model mode. (sizeof int == 2, and sizeof int* is 4.)

In a test here, I get:

*b = 1 *c = 1
*b = 2 *c = 3
*b = 3 *c = 5
*b = 4 *c = 7
*b = 5 *c = 9
*b = 6 *c = 2013
*b = 7 *c = 2013
*b = 8 *c = 9
*b = 9 *c = 2680
So, printf would really like a %p (at least) for the third arg.




虽然它仍会打印(基本上)相同的东西,因为* c是

仍然实际上一个int被视为int *。

无论如何,这可能属于UB的范围。标题,因为你是

采取int'并将它们视为int *'。


(是的,我知道撇号不是''真的属于最后一句话,

但它让它更清晰,恕我直言。)

-

+ ----- -------------------- + -------------------- + -------- --------------------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

不要给我发电子邮件:< mailto:Th ************* @ gmail.com>



Though it would still print (basically) the same thing, as *c is
still really an int being treated as int*.
In any case, this probably falls under the "UB" heading, as you are
taking int''s and treating them as int*''s.

(Yes, I know the apostrophes don''t really belong in the last sentence,
but it makes it more legible, IMHO.)
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>


这篇关于解释为什么打印相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆