i**p 发帖数: 902 | 1 Could anybody explain why statement 3 works?
char abc[30];
1. strcpy(&(abc[0], "Hello");
2. strcpy(abc ,"Hello");
3. strcpy(&abc ,"Hello"); |
k****f 发帖数: 3794 | 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");
|
t****t 发帖数: 6806 | 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");
|
i**p 发帖数: 902 | 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
|
b******a 发帖数: 215 | 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.
|
t****t 发帖数: 6806 | 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.
|
k****f 发帖数: 3794 | 7 哈哈,你太nice了。
【在 t****t 的大作中提到】 : comment: you are wrong, and please read the whole chapter of FAQ.
|
t****t 发帖数: 6806 | 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)
|
|
b******a 发帖数: 215 | 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.
|
o**o 发帖数: 3964 | 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.
|
p*u 发帖数: 2454 | 11 呵呵,it天天守在这。
【在 b******a 的大作中提到】 : 哈哈。又被你老给找出毛病来了。
|