在scanf中使用&数组 [英] Using &array with scanf

查看:86
本文介绍了在scanf中使用&数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char array [20];

scanf("%19s",& array);


我知道这是错的,因为它' 'sa类型不匹配,其中scanf期望

a指向char的指针并获取指向20 char数组的指针。我知道

C问题6.12的问题说这对于那个非常好的原因是错误的。我不知道的是,标准最终告诉我

这是错的。我不知道的是这种类型

不匹配会在实践中破裂。


最近一位同伴问我,为什么我错了告诉他,这是错误的,我感到非常不舒服,因为我知道这是错的,当他让我证明这件事的时候我没有好的答案。那么我如何向一个特别顽固的程序员证明这一点呢?b
有黑白证据以及失败的真实例子吗?


谢谢!

char array[20];
scanf("%19s", &array);

I know this is wrong because it''s a type mismatch, where scanf expects
a pointer to char and gets a pointer to an array of 20 char. I know
that question 6.12 of the C FAQ says that it''s wrong for that very
reason. What I don''t know is where the standard tells me conclusively
that it''s wrong. What I also don''t know is somewhere that this type
mismatch will break in practice.

A peer asked me recently why it was wrong when I told him that it was
wrong, and I was very uncomfortable because I know it''s wrong and I had
no good answer when he asked me to prove it. So how do I prove
something this to an exceptionally stubborn programmer who wants to
have black and white proof as well as a real example that fails?

Thanks!

推荐答案

James Daughtry说:
James Daughtry said:
char array [20];
scanf("%19s",& array);

我知道这是错误的,因为它是类型不匹配,其中scanf期望
指向char的指针,获取指向20 char数组的指针。


是的。这也是错误的,因为它没有检查scanf的返回值。

我知道C FAQ的问题6.12说这是非常错的
原因。


是的。

我不知道的是标准最终告诉我它的错误。


fscanf规范:


s匹配一系列非空白字符。相应的

参数应该是一个指向数组初始字符的指针

足以接受序列和一个终止空字符,这就是

将自动添加。


&数组不是指向数组初始字符的指针,大小足以接受序列;它是指向整个数组的指针。不同的类型。

这违反了应该。在约束之外,所以行为是

undefined。

我也不知道这个类型
不匹配会在实践中破坏的地方。


不相关。打破它的符合要求的实施可以在明天发布



一位同事最近问我,为什么当我告诉他这是错误的时候错了,我非常不舒服,因为我知道这是错的,当他要我证明时,我没有好的答案。
char array[20];
scanf("%19s", &array);

I know this is wrong because it''s a type mismatch, where scanf expects
a pointer to char and gets a pointer to an array of 20 char.
Yup. It''s also wrong because it doesn''t check the return value of scanf.
I know that question 6.12 of the C FAQ says that it''s wrong for that very
reason.
Yup.
What I don''t know is where the standard tells me conclusively that it''s
wrong.
The fscanf specification:

s Matches a sequence of non-white-space characters. The corresponding
argument shall be a pointer to the initial character of an array large
enough to accept the sequence and a terminating null character, which
will be added automatically.

&array is not a pointer to the initial character of an array large enough to
accept the sequence; it''s a pointer to an entire array. Different type.
That''s a violation of a "shall" outside a constraint, so the behaviour is
undefined.
What I also don''t know is somewhere that this type
mismatch will break in practice.
Irrelevant. A conforming implementation which breaks it could be released
tomorrow.
A peer asked me recently why it was wrong when I told him that it was
wrong, and I was very uncomfortable because I know it''s wrong and I had
no good answer when he asked me to prove it.




请他解释他怎么可能混淆char(*)[20]和char *,

,因为它们是完全不同的类型,具有完全不同的

尺寸。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



Ask him to explain how he can possibly confuse a char (*)[20] and a char *,
given that they are completely different types with completely different
sizes.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)




2006年1月5日05:12:32 -0800,

James Daughtry写道:
Hi,

At 5 Jan 2006 05:12:32 -0800,
James Daughtry wrote:

char array [20];
scanf("%19s"& array);

一位同事最近问我,为什么当我告诉他这是错的时候错了,我很不舒服因为我知道这是错的,当他要我证明时,我没有好的答案。

char array[20];
scanf("%19s", &array);

A peer asked me recently why it was wrong when I told him that it was
wrong, and I was very uncomfortable because I know it''s wrong and I had
no good answer when he asked me to prove it.




&数组是一个指向指针的指针到数组的第一个元素,它是类型为char **的
,而不是char *。

你可以使用

scanf(" %19s",array);



scanf("%19s",& array [0]);

问候,

Roland

-

Roland Csaszar ----------- \\\\ /// -------------- +43 316 495 2129

软件开发------ \\\ /// --- -------- http://www.knapp.com
KNAPP物流自动化 - \\V // - mailto:ro ************ @ knapp.com



&array is a pointer to the pointer to the first element of array, it is
of type char**, not char*.
You can use
scanf ("%19s", array);
or
scanf ("%19s", &array[0]);
Regards,
Roland
--
Roland Csaszar ----------- \\\ /// -------------- +43 316 495 2129
Software Development ------ \\\ /// ----------- http://www.knapp.com
KNAPP Logistics Automation - \\V// - mailto:ro************@knapp.com


Richard Heathfield明智地写道:
Richard Heathfield wisely wrote:
James Daughtry说:
James Daughtry said:
char array [20];
scanf("%19s",&数组);

我知道这是错误的,因为它是类型不匹配,其中scanf需要指向char的指针并获取指向20个字符的数组的指针。
对。这也是错误的,因为它没有检查scanf的返回值。
char array[20];
scanf("%19s", &array);

I know this is wrong because it''s a type mismatch, where scanf expects
a pointer to char and gets a pointer to an array of 20 char.
Yup. It''s also wrong because it doesn''t check the return value of scanf.




是的,我不想用错误检查来淡化示例。

幸运的是,我无意编译那个片段,而且我不会想象它会在作为Usenet帖子执行时造成任何伤害。 ;-)



Yea, I didn''t want to dilute the example with error checking.
Fortunately, I have no intention of compiling that snippet, and I don''t
imagine it will do any harm when executed as a Usenet post. ;-)

我知道C FAQ的问题6.12说这是非常错误的原因。
I know that question 6.12 of the C FAQ says that it''s wrong for that very
reason.



是的。



Yup.

我不知道的是标准最终告诉我它的错误。
What I don''t know is where the standard tells me conclusively that it''s
wrong.



fscanf规范:

s匹配一系列非空白字符。相应的
参数应该是一个指向数组初始字符的指针,该数组足以接受序列和终止空字符,这将自动添加。

&数组不是一个指向数组初始字符的指针,该数组大到足以接受序列;它是指向整个数组的指针。不同的类型。
这是违反应该的。在约束之外,所以行为是未定义的。



The fscanf specification:

s Matches a sequence of non-white-space characters. The corresponding
argument shall be a pointer to the initial character of an array large
enough to accept the sequence and a terminating null character, which
will be added automatically.

&array is not a pointer to the initial character of an array large enough to
accept the sequence; it''s a pointer to an entire array. Different type.
That''s a violation of a "shall" outside a constraint, so the behaviour is
undefined.




啊,现在这是我的问题。我直截了当地告诉那个段落,

证明了我的观点,结果是一个快速的程序,就像下面的

一样。他试图告诉我通过该程序的输出

该数组和&数组导致相同的地址,并且该类型不会因为scanf而导致b $ b会把同一个地址看成是指向

char的指针,而且类型不匹配是无关紧要的。


#include< stdio.h>


int main(无效)

{

char数组[20];


printf( "%p \ n%p \ n",(void *)& array,(void *)array);


返回0;

}


就像我说的那样,他是一个顽固的小家伙。



Ah, now that''s my problem. I made a beeline to that very paragraph to
prove my point, and the result was a quickie program much like the
following. He was trying to tell me through the output of the program
that array and &array result in the same address, and the type doesn''t
matter because scanf will treat the same address like a pointer to
char, and the type mismatch is irrelevant.

#include <stdio.h>

int main(void)
{
char array[20];

printf("%p\n%p\n", (void*)&array, (void*)array);

return 0;
}

Like I said, he''s a stubborn little bugger.

我也是不知道在某种程度上这种类型的不匹配会在实践中破裂。
What I also don''t know is somewhere that this type
mismatch will break in practice.



不相关。一个符合要求的实现打破了它可以明天发布。



Irrelevant. A conforming implementation which breaks it could be released
tomorrow.




不幸的是,他是那种使用它适合我的人;

参数。我知道他错了,你知道他错了,但是他拒绝承认他是错的,直到我能写出证明他的程序

错了。 :-



Unfortunately, he''s the kind of person who uses the "it works for me"
argument. I know he''s wrong, you know he''s wrong, but he refuses to
admit that he''s wrong until I can write a program that proves him
wrong. :-P

一位同伴最近问我,为什么当我告诉他这是错的时候是错的,我感到非常不舒服因为我知道这是错的,当他要我证明时,我没有好的答案。
A peer asked me recently why it was wrong when I told him that it was
wrong, and I was very uncomfortable because I know it''s wrong and I had
no good answer when he asked me to prove it.



请他解释他怎么可能混淆一个字符( *)[20]和char *,
因为它们是完全不同的类型,具有完全不同的尺寸。



Ask him to explain how he can possibly confuse a char (*)[20] and a char *,
given that they are completely different types with completely different
sizes.




我几乎问他同样的问题。我问为什么它应该工作当

不同类型的指针不需要具有相同的

表示,即使它们指向相同的地址,增加重点

指出标准的相关部分。坚持下去,

他再次运行该程序并说地址是相同的,然后

运行一个不正确的scanf示例来证明它的工作方式是他的/>
预计,并重复scanf将在内部进行隐式转换



-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上面的域名(但显然放弃了www)



I asking him almost the same question. I asked why it should work when
pointers of different types aren''t required to have the same
representation even if they point to the same address, adding emphasis
by pointing out the relevant parts of the standard. Holding his ground,
he ran the program again and said that the addresses are the same, then
ran an incorrect scanf example to prove that it worked the way he
expected, and repeated that scanf will do an implicit conversion
internally.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)






这篇关于在scanf中使用&amp;数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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