我只是想不通 strcat [英] I just can't figure out strcat

查看:16
本文介绍了我只是想不通 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" so 你应该尝试类似 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天全站免登陆