&符号和C中的scanf? [英] Ampersand(&) and scanf in C?
问题描述
我有以下多项选择题,我不能弄清楚为什么(A)和(C)不正确,请多加解释!下一个问题中唯一的正确答案是(B).
I have the following multiple-choice question and I cannot figure out why (A) and (C) are incorrect, any explanation would be appreciated! The only correct answer in the following question is (B).
以下哪些是scanf的正确用法?
(A)int i=0; scanf("%d", i);
(B)int i=0; int *p=&i; scanf("%d", p);
(C)char *s="1234567"; scanf("%3s", &s);
(D)char c; scanf("%c", c);
Which of the following is a correct usage of scanf?
(A)int i=0; scanf("%d", i);
(B)int i=0; int *p=&i; scanf("%d", p);
(C)char *s="1234567"; scanf("%3s", &s);
(D)char c; scanf("%c", c);
推荐答案
scanf
希望在正确的地址存储结果:
scanf
wants a correct address where to store the result:
(A) int i=0; scanf("%d", i);
i
是按值传递的,没有地址:错误.
i
is passed here by value, no address: wrong.
(B) int i=0; int *p=&i; scanf("%d", p);
p
是指向int的指针,scanf
可以将其结果存储在此处:对
p
is a pointer to int, scanf
can store its result here: right
(C) char *s="1234567"; scanf("%3s", &s);
&s
是指向char *
的指针的地址,您不能在其中存储字符串:错误
&s
is the address of a pointer to char *
, you cannot store a string there: wrong
(D) char c; scanf("%c", c);
c
是通过值而不是地址传递的:错误
c
is passed by value, not an address: wrong
这篇关于&符号和C中的scanf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!