由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - strcat()
相关主题
数组问题C pass string 问题
问个字符串的基本问题a string define question (c++)
Array in C请教一个基础C++问题
关于 exception 的一个问题C语言的变量都一定要放在stack上吗?
呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题问一个C++函数Parameter的问题
C 语言,初学者,简单问题(2)what's wrong with this C++ code?
谁来解释一下这个是compiler问题吗?C语言重复定义问题
C++ STL map find does not work ???[合集] 简单问题一个:为什么可以间接访问私有指针成员
相关话题的讨论汇总
话题: s1话题: char话题: strcat话题: hello话题: s3
进入Programming版参与讨论
1 (共1页)
i**p
发帖数: 902
1
char s1[] = "Hello ";
char s2[] = "world";
char *s3;
s3 = strcat(s1, s2);
It is Ok. Both s1 and s3 will be "Hello world".
char *s1 = "Hello ";
char s2[] = "world";
char *s3;
s3 = strcat(s1, s2);
It is runtime error. Can you explain?
P********e
发帖数: 2610
2
strcat(char*, const char*)
s1必须是char *;
char *s1 = "hello";
s1是指向 const char*的,因为s1是desti

【在 i**p 的大作中提到】
: char s1[] = "Hello ";
: char s2[] = "world";
: char *s3;
: s3 = strcat(s1, s2);
: It is Ok. Both s1 and s3 will be "Hello world".
: char *s1 = "Hello ";
: char s2[] = "world";
: char *s3;
: s3 = strcat(s1, s2);
: It is runtime error. Can you explain?

i**p
发帖数: 902
3
Do you mean
char *s1 = "hello"; s1 points to a const string?

【在 P********e 的大作中提到】
: strcat(char*, const char*)
: s1必须是char *;
: char *s1 = "hello";
: s1是指向 const char*的,因为s1是desti

P********e
发帖数: 2610
4


【在 i**p 的大作中提到】
: Do you mean
: char *s1 = "hello"; s1 points to a const string?

d****2
发帖数: 6250
5
you totally mess up the memory. s1 must be allocated with enough space first
.
i**p
发帖数: 902
6
How about
char s1[] = "hello";
I think the size of s1 has been set to 6. how does it work?

【在 P********e 的大作中提到】
: 恩
i**p
发帖数: 902
7
How does it work?
char s1[] = "Hello ";
char s2[] = "world";
char *s3;
s3 = strcat(s1, s2);
Both s1 and s3 will be "Hello world".

【在 i**p 的大作中提到】
: How about
: char s1[] = "hello";
: I think the size of s1 has been set to 6. how does it work?

d****2
发帖数: 6250
8

it does not work. any c programmer writes this way should be hanged.

【在 i**p 的大作中提到】
: How does it work?
: char s1[] = "Hello ";
: char s2[] = "world";
: char *s3;
: s3 = strcat(s1, s2);
: Both s1 and s3 will be "Hello world".

i**p
发帖数: 902
9
I run it by VC. It prints both s1 and s3 as "hello world".

【在 d****2 的大作中提到】
:
: it does not work. any c programmer writes this way should be hanged.

t****t
发帖数: 6806
10
运行结果正确不代表你的程序就是正确的

【在 i**p 的大作中提到】
: I run it by VC. It prints both s1 and s3 as "hello world".
相关主题
C 语言,初学者,简单问题(2)C pass string 问题
谁来解释一下这个是compiler问题吗?a string define question (c++)
C++ STL map find does not work ???请教一个基础C++问题
进入Programming版参与讨论
i**p
发帖数: 902
11
then what tool can help us to find this programming error?

【在 t****t 的大作中提到】
: 运行结果正确不代表你的程序就是正确的
d****2
发帖数: 6250
12

that is a good research question. MSFT will be very interested to fix their
crappy code base.

【在 i**p 的大作中提到】
: then what tool can help us to find this programming error?
i**p
发帖数: 902
13
A good compiler should at least give a waring, but VC++ does not.

their

【在 d****2 的大作中提到】
:
: that is a good research question. MSFT will be very interested to fix their
: crappy code base.

d****2
发帖数: 6250
14

google buffer overflow.

【在 i**p 的大作中提到】
: A good compiler should at least give a waring, but VC++ does not.
:
: their

c*****t
发帖数: 1879
15
Just forget using strcat function. sprintf and strcpy are
less error prone.

【在 i**p 的大作中提到】
: A good compiler should at least give a waring, but VC++ does not.
:
: their

t****t
发帖数: 6806
16
name a compiler that will give a warning for this.

【在 i**p 的大作中提到】
: A good compiler should at least give a waring, but VC++ does not.
:
: their

m*****e
发帖数: 4193
17

It's not ok. You just got lucky.

【在 i**p 的大作中提到】
: A good compiler should at least give a waring, but VC++ does not.
:
: their

O*******d
发帖数: 20343
18
如果你把s1和s2中间插一个,看看能不能运行。再看一看_s1变成了什么。
char s1[] = "Hello ";
char _s1[] = "whatever";
char s2[] = "world";
char *s3;
s3 = strcat(s1, s2);
printf("s1 %s _s1 %s\n", s1, _s1);

【在 i**p 的大作中提到】
: I run it by VC. It prints both s1 and s3 as "hello world".
i**p
发帖数: 902
19
I know what all of you talk about. see my original post. My qustion is s1[]
and *s1 have different result, which means *s1 is more protective than s1[].
Why not make them the same?
char s1[] = "Hello ";
char *s1 = "Hello ";

【在 O*******d 的大作中提到】
: 如果你把s1和s2中间插一个,看看能不能运行。再看一看_s1变成了什么。
: char s1[] = "Hello ";
: char _s1[] = "whatever";
: char s2[] = "world";
: char *s3;
: s3 = strcat(s1, s2);
: printf("s1 %s _s1 %s\n", s1, _s1);

d****2
发帖数: 6250
20
char [] has space allocated on stack
char * is a pointer to static text area, write protected.
BTW, char [] and char * are different thing. Compiler does some translation
for you so you won't notice the difference most of time. Try sizeof(s1) in
both cases and you will know why.
相关主题
C语言的变量都一定要放在stack上吗?C语言重复定义问题
问一个C++函数Parameter的问题[合集] 简单问题一个:为什么可以间接访问私有指针成员
what's wrong with this C++ code?[合集] 6个变态的C语言写的Hello World (ZZ)
进入Programming版参与讨论
i**p
发帖数: 902
21
Of course, s1[] and *s1 are different. I have seen lots of programmer tried
to do something like “strcpy (s2, s1, sizeof(s1))” with s1 defined as *s1.
When is happened, I partly blame the programmer and mostly blame why C does
not do something to prevent this from the beginning or a latter improvement
. Anyway, this is a dangerous pitfall for a beginner and mostly to be an
interview question.
For the strcat(), it is much easier to generate a compiler warning or
runtime error for s1[] than *s1, b

【在 d****2 的大作中提到】
: char [] has space allocated on stack
: char * is a pointer to static text area, write protected.
: BTW, char [] and char * are different thing. Compiler does some translation
: for you so you won't notice the difference most of time. Try sizeof(s1) in
: both cases and you will know why.

t****t
发帖数: 6806
22
在你指责一个语言有这样那样的问题的时候, 有必要考虑语言设计时的历史环境. 为了
保持高速度, C语言里本来就没有数组边界检查, 更何况数组在函数调用里自动退化为指
针,数组尺寸信息早就没有了.
嫌C语言不安全的话,有很多更安全的语言可以用.就算是C++, 也有string class.
不过, 会在strcpy()里写上三个参数的, 可以想见你连门都没入. (FYI, 有三个参数的
那个叫做strncpy().) 门都没入的人还是虚心一点的好, 不要连纸上谈兵都谈不来,被人
笑掉大牙.

tried
s1.
does
improvement
an

【在 i**p 的大作中提到】
: Of course, s1[] and *s1 are different. I have seen lots of programmer tried
: to do something like “strcpy (s2, s1, sizeof(s1))” with s1 defined as *s1.
: When is happened, I partly blame the programmer and mostly blame why C does
: not do something to prevent this from the beginning or a latter improvement
: . Anyway, this is a dangerous pitfall for a beginner and mostly to be an
: interview question.
: For the strcat(), it is much easier to generate a compiler warning or
: runtime error for s1[] than *s1, b

d****2
发帖数: 6250
23
funny, OP is asking beginner questions yet acts like a pro wannabe. no wonder such forums are boring. compiler is harder than you think.
i**p
发帖数: 902
24
小屁孩拣了个大包子,乐得找不到了北。老哥虽民工出身,码砖的事早就不干了。听说
过大博士不知道 10000000是负几吗?没听说过只能说你太嫩。我到是在民工
学徒其间老听到同道笑话师傅打字没他快,你也就和他有一比了。

为指
被人

【在 t****t 的大作中提到】
: 在你指责一个语言有这样那样的问题的时候, 有必要考虑语言设计时的历史环境. 为了
: 保持高速度, C语言里本来就没有数组边界检查, 更何况数组在函数调用里自动退化为指
: 针,数组尺寸信息早就没有了.
: 嫌C语言不安全的话,有很多更安全的语言可以用.就算是C++, 也有string class.
: 不过, 会在strcpy()里写上三个参数的, 可以想见你连门都没入. (FYI, 有三个参数的
: 那个叫做strncpy().) 门都没入的人还是虚心一点的好, 不要连纸上谈兵都谈不来,被人
: 笑掉大牙.
:
: tried
: s1.

m*****e
发帖数: 4193
25
Please take your troll somewhere else.

【在 i**p 的大作中提到】
: 小屁孩拣了个大包子,乐得找不到了北。老哥虽民工出身,码砖的事早就不干了。听说
: 过大博士不知道 10000000是负几吗?没听说过只能说你太嫩。我到是在民工
: 学徒其间老听到同道笑话师傅打字没他快,你也就和他有一比了。
:
: 为指
: 被人

t****t
发帖数: 6806
26
虽然不知道你说的是啥,不过常来的都知道,我不是靠写程序吃饭的

【在 i**p 的大作中提到】
: 小屁孩拣了个大包子,乐得找不到了北。老哥虽民工出身,码砖的事早就不干了。听说
: 过大博士不知道 10000000是负几吗?没听说过只能说你太嫩。我到是在民工
: 学徒其间老听到同道笑话师傅打字没他快,你也就和他有一比了。
:
: 为指
: 被人

n******t
发帖数: 4406
27
你真是小母牛选美啊。

【在 i**p 的大作中提到】
: 小屁孩拣了个大包子,乐得找不到了北。老哥虽民工出身,码砖的事早就不干了。听说
: 过大博士不知道 10000000是负几吗?没听说过只能说你太嫩。我到是在民工
: 学徒其间老听到同道笑话师傅打字没他快,你也就和他有一比了。
:
: 为指
: 被人

i**p
发帖数: 902
28
Please understand it is not shame to 靠写程序吃饭. My point is you pay more
attention to my example strcpy. Thank you anyway since you posted a lot for
this topic even though I do not agree most of your answer.

【在 t****t 的大作中提到】
: 虽然不知道你说的是啥,不过常来的都知道,我不是靠写程序吃饭的
f**y
发帖数: 138
29
You guys don't mention s2 at all? It probably will be "orld".

【在 i**p 的大作中提到】
: Please understand it is not shame to 靠写程序吃饭. My point is you pay more
: attention to my example strcpy. Thank you anyway since you posted a lot for
: this topic even though I do not agree most of your answer.

1 (共1页)
进入Programming版参与讨论
相关主题
[合集] 简单问题一个:为什么可以间接访问私有指针成员呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题
[合集] 6个变态的C语言写的Hello World (ZZ)C 语言,初学者,简单问题(2)
inline到底能省多少时间?谁来解释一下这个是compiler问题吗?
求GCC高手C++ STL map find does not work ???
数组问题C pass string 问题
问个字符串的基本问题a string define question (c++)
Array in C请教一个基础C++问题
关于 exception 的一个问题C语言的变量都一定要放在stack上吗?
相关话题的讨论汇总
话题: s1话题: char话题: strcat话题: hello话题: s3