avatar
i*p
1
Could anybody explain why statement 3 works?
char abc[30];
1. strcpy(&(abc[0], "Hello");
2. strcpy(abc ,"Hello");
3. strcpy(&abc ,"Hello");
avatar
k*f
2
works不等于正确,
会有后遗症的

【在 i**p 的大作中提到】
: Could anybody explain why statement 3 works?
: char abc[30];
: 1. strcpy(&(abc[0], "Hello");
: 2. strcpy(abc ,"Hello");
: 3. strcpy(&abc ,"Hello");

avatar
t*t
3
http://c-faq.com/aryptr/aryvsadr.html

【在 i**p 的大作中提到】
: Could anybody explain why statement 3 works?
: char abc[30];
: 1. strcpy(&(abc[0], "Hello");
: 2. strcpy(abc ,"Hello");
: 3. strcpy(&abc ,"Hello");

avatar
i*p
4
So based on the explaniation in this web page, I have following:
char abc[30];
abc reference to a has type ``pointer to char,'' and &abc is ``pointer to
array of 10 chars.''
we should use &abc for a string, but mostly we use abc. Please comment.

【在 t****t 的大作中提到】
: http://c-faq.com/aryptr/aryvsadr.html
avatar
b*a
5
actually &abc and abc point to the same address.
the different is when you set
char (*p1)[30]=&abc;
p1+=1;
char* p2=abc;
p2+=1;
p1+1 and p2+1 will point to different address,
say p2=p1=0x000010, p1+1 will make p1 point to 0x00010+ 1E*sizeof(char);
p2+1 will point to 0x00010+1*sizeof(char)

【在 i**p 的大作中提到】
: So based on the explaniation in this web page, I have following:
: char abc[30];
: abc reference to a has type ``pointer to char,'' and &abc is ``pointer to
: array of 10 chars.''
: we should use &abc for a string, but mostly we use abc. Please comment.

avatar
t*t
6
comment: you are wrong, and please read the whole chapter of FAQ.

【在 i**p 的大作中提到】
: So based on the explaniation in this web page, I have following:
: char abc[30];
: abc reference to a has type ``pointer to char,'' and &abc is ``pointer to
: array of 10 chars.''
: we should use &abc for a string, but mostly we use abc. Please comment.

avatar
k*f
7
哈哈,你太nice了。

【在 t****t 的大作中提到】
: comment: you are wrong, and please read the whole chapter of FAQ.
avatar
t*t
8
that... really depends on the type of p1 and p2. if p1 and p2 are of the
same type,
your p1+1 and p2+1 will be the same anyway. if you say (&abc)+1 and abc+1
are different, that makes more sense.

【在 b******a 的大作中提到】
: actually &abc and abc point to the same address.
: the different is when you set
: char (*p1)[30]=&abc;
: p1+=1;
: char* p2=abc;
: p2+=1;
: p1+1 and p2+1 will point to different address,
: say p2=p1=0x000010, p1+1 will make p1 point to 0x00010+ 1E*sizeof(char);
: p2+1 will point to 0x00010+1*sizeof(char)

avatar
b*a
9
哈哈。又被你老给找出毛病来了。

【在 t****t 的大作中提到】
: that... really depends on the type of p1 and p2. if p1 and p2 are of the
: same type,
: your p1+1 and p2+1 will be the same anyway. if you say (&abc)+1 and abc+1
: are different, that makes more sense.

avatar
o*o
10
why do you guys need +1 in the first place? didn't see that in OP's post

【在 t****t 的大作中提到】
: that... really depends on the type of p1 and p2. if p1 and p2 are of the
: same type,
: your p1+1 and p2+1 will be the same anyway. if you say (&abc)+1 and abc+1
: are different, that makes more sense.

avatar
p*u
11
呵呵,it天天守在这。

【在 b******a 的大作中提到】
: 哈哈。又被你老给找出毛病来了。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。