我在用C语言工作,我必须把一些东西连接起来。
现在我有这个:
message = strcat("TEXT ", var);
message2 = strcat(strcat("TEXT ", foo), strcat(" TEXT ", bar));
现在,如果你有C语言的经验,我相信你会意识到,当你试图运行它时,这会给你一个分割错误。我该怎么做呢?
我在用C语言工作,我必须把一些东西连接起来。
现在我有这个:
message = strcat("TEXT ", var);
message2 = strcat(strcat("TEXT ", foo), strcat(" TEXT ", bar));
现在,如果你有C语言的经验,我相信你会意识到,当你试图运行它时,这会给你一个分割错误。我该怎么做呢?
当前回答
假设你有一个char[fixed_size]而不是char*,你可以使用一个单一的,创造性的宏来一次性完成它,使用<<cout<<,比如排序(“而不是%s这个脱节的%s\n”,“而不是”,“printf样式格式”)。如果您正在使用嵌入式系统,此方法还允许您省略malloc和大型*printf函数家族,如snprintf()(这使dietlibc也不会抱怨*printf)
#include <unistd.h> //for the write example
//note: you should check if offset==sizeof(buf) after use
#define strcpyALL(buf, offset, ...) do{ \
char *bp=(char*)(buf+offset); /*so we can add to the end of a string*/ \
const char *s, \
*a[] = { __VA_ARGS__,NULL}, \
**ss=a; \
while((s=*ss++)) \
while((*s)&&(++offset<(int)sizeof(buf))) \
*bp++=*s++; \
if (offset!=sizeof(buf))*bp=0; \
}while(0)
char buf[256];
int len=0;
strcpyALL(buf,len,
"The config file is in:\n\t",getenv("HOME"),"/.config/",argv[0],"/config.rc\n"
);
if (len<sizeof(buf))
write(1,buf,len); //outputs our message to stdout
else
write(2,"error\n",6);
//but we can keep adding on because we kept track of the length
//this allows printf-like buffering to minimize number of syscalls to write
//set len back to 0 if you don't want this behavior
strcpyALL(buf,len,"Thanks for using ",argv[0],"!\n");
if (len<sizeof(buf))
write(1,buf,len); //outputs both messages
else
write(2,"error\n",6);
注1,你通常不会像这样使用argv[0]——只是一个例子 注意2,您可以使用输出char*的任何函数,包括用于将整数转换为字符串类型的itoa()等非标准函数。 注意3,如果你已经在程序的任何地方使用了printf,那么没有理由不使用snprintf(),因为编译后的代码会更大(但内联且明显更快)。
其他回答
如果你有C语言的经验,你会注意到字符串只是字符数组,其中最后一个字符是空字符。
现在这是相当不方便的,因为你必须找到最后一个字符,以追加一些东西。Strcat会帮你的。
所以strcat通过第一个参数搜索空字符。然后它将用第二个参数的内容替换它(直到它以null结束)。
现在让我们看一下你的代码:
message = strcat("TEXT " + var);
在这里,您向指向文本“text”的指针添加了一些内容(“text”的类型是const char*。一个指针)。
这通常是行不通的。此外,修改“TEXT”数组也将不起作用,因为它通常被放置在常量段中。
message2 = strcat(strcat("TEXT ", foo), strcat(" TEXT ", bar));
这可能会工作得更好,除非您再次尝试修改静态文本。Strcat没有为结果分配新的内存。
我建议这样做:
sprintf(message2, "TEXT %s TEXT %s", foo, bar);
阅读sprintf的文档,检查它的选项。
现在有一点很重要:
确保缓冲区有足够的空间容纳文本和空字符。有几个函数可以帮助您,例如strncat和printf的特殊版本,它们为您分配缓冲区。 不确保缓冲区大小将导致内存损坏和可远程利用的错误。
您正在尝试将字符串复制到静态分配的地址中。你需要进入缓冲地带。
具体地说:
剪断…
目的地
Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
剪断…
http://www.cplusplus.com/reference/clibrary/cstring/strcat.html
这里也有一个例子。
另外,如果您不知道要连接多少字符串,malloc和realloc也很有用。
#include <stdio.h>
#include <string.h>
void example(const char *header, const char **words, size_t num_words)
{
size_t message_len = strlen(header) + 1; /* + 1 for terminating NULL */
char *message = (char*) malloc(message_len);
strncat(message, header, message_len);
for(int i = 0; i < num_words; ++i)
{
message_len += 1 + strlen(words[i]); /* 1 + for separator ';' */
message = (char*) realloc(message, message_len);
strncat(strncat(message, ";", message_len), words[i], message_len);
}
puts(message);
free(message);
}
字符串也可以在编译时进行连接。
#define SCHEMA "test"
#define TABLE "data"
const char *table = SCHEMA "." TABLE ; // note no + or . or anything
const char *qry = // include comments in a string
" SELECT * " // get all fields
" FROM " SCHEMA "." TABLE /* the table */
" WHERE x = 1 " /* the filter */
;
在C语言中,“字符串”只是简单的字符数组。因此,您不能直接将它们与其他“字符串”连接。
你可以使用strcat函数,它将src指向的字符串追加到dest指向的字符串的末尾:
char *strcat(char *dest, const char *src);
下面是一个来自cplusplus.com的例子:
char str[80];
strcpy(str, "these ");
strcat(str, "strings ");
strcat(str, "are ");
strcat(str, "concatenated.");
对于第一个参数,需要提供目标缓冲区本身。目标缓冲区必须是字符数组缓冲区。例如:char buffer[1024];
确保第一个参数有足够的空间存储要复制的内容。如果可以,使用像:strcpy_s和strcat_s这样的函数更安全,其中必须显式指定目标缓冲区的大小。
注意:字符串字面值不能用作缓冲区,因为它是常量。因此,您总是必须为缓冲区分配一个char数组。
strcat的返回值可以简单地忽略,它只返回与作为第一个参数传入的指针相同的指针。它的存在是为了方便,并允许你将调用链接到一行代码中:
strcat(strcat(str, foo), bar);
所以你的问题可以这样解决:
char *foo = "foo";
char *bar = "bar";
char str[80];
strcpy(str, "TEXT ");
strcat(str, foo);
strcat(str, bar);