我只是不知道strcat [英] I just can't figure out strcat

查看:102
本文介绍了我只是不知道strcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我不应该使用该功能,并且我不在乎。上次我检查strcat的规范时,它说了一些类似的操作,它们可以更新第一个值并返回相同的值。

I know I shouldn't be using that function, and I don't care. Last time I checked the spec for strcat, it said something along the lines of updating the first value as well as returning the same.

现在,这是一个非常愚蠢的问题,我想让您像在跟一个非常愚蠢的人说话一样来解释它。

Now, this is a really stupid question, and I want you to explain it like you're talking to a really stupid person.

为什么这样行不通?

char* foo="foo";
printf(strcat(foo,"bar"));

编辑:我不知道char []和char *之间的区别。我该如何分配255个字符的字符串?

I don't know the difference between char[] and char*. How would I allocate a string of 255 characters?

编辑2:好,好,所以char [number]分配那么多字节的字符串?说得通。谢谢。

EDIT 2: OK, OK, so char[number] allocates a string of that many bytes? Makes sense. Thanks.

编辑3:另外,如何在不声明字符数组的情况下使用它?我将其转换为char [255]吗?

EDIT 3: Also, how would I use a character array without declaring it? Would I typecast it as char[255]?

编辑4:strcat((char [256]) foo, bar)返回错误。我要受够了C。

EDIT 4: strcat((char[256])"foo","bar") returns an error. I'm about fed up with C.

编辑5:strcat((char [256]) foo,(char []) bar)也是如此。

EDIT 5: So does strcat((char[256])"foo",(char[])"bar").

编辑5:

char[256] foo="bar";

真正平滑。 期望标识符

Real smooth. "identifier expected"

推荐答案

char* foo="foo";

是字符串文字。

无法进行修改。做 char [] foo = foo; ,但是要记住,像这样使用strcat会导致问题,因为这种情况会在内存中写入,不应这样做尝试 strcat bar 如此时,您应该尝试类似 char foo [30] = foo;

It can't be modified. Do char [] foo = "foo"; instead but keep in mind that using strcat like that, will cause problems in this case cause it will write in memory it shouldn't when trying to strcat "bar" so you should try something like char foo[30] = "foo";

编辑:您正在做的类型转换...对不起,我没有那么多的大脑细胞来尝试向您解释您的意图做。我只能告诉你这是错误的。您需要提供一个内存位置,以便 strcat()可以工作。

The typecasting you do... sorry I do not have so many brain cells as to try to explain you what you are trying to do. I can only tell you it is wrong. you need to provide a memory location so strcat() can work.

尝试

int main()
{
    char foo[255]="foo";
    printf("%s",strcat(foo,"bar"));
}

这篇关于我只是不知道strcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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