Tag: string concatenation

了解C字符串连接

对于C中的评估,我必须在其中包含两个带有字符串的文件,并使用一个字符串将它们连接在一起,例如: 如果file1包含 Now is the time for all good men to come to the aid of the party. 和file2包含: alpha beta gamma 那么输出来自 scat XX file1 file2 (scat是程序名称) 应该 Now is theXXalpha time for allXXbeta good men toXXgamma come to the aid ofXX the party.XX 和输出 scat XX file2 file1 应该 alphaXXNow is the betaXXtime […]

我应该如何为c-string char数组分配内存?

因此,在尝试学习如何在C ++中使用C-Strings时,我遇到了内存分配问题。 这里的想法是创建一个新的字符串格式(s1 + sep + s2)我正在使用的文本提供了标题,所以我不能改变它,但我遇到了试图设置大小的问题char str []。 我收到一个错误,说sLength不是常量,因此不能用于设置数组的大小。 我对C ++比较陌生,所以这是一个两部分问题。 这个策略实际上是为新arrays分配内存吗? 如果使用strlen(char *)无法获得常量值,如何正确设置数组大小? char* concatStrings(char* s1, char* s2, char sep){ int sLength = strlen(s1) + strlen(s2) + 3; //+1 for char sep +2 for \0 at end of string char *str = new char[sLength]; strcpy (str, s1); str [sLength(s1)] = sep; strcat (str, […]

连接两个char数组?

如果我有两个char数组,如下所示: char one[200]; char two[200]; 然后我想制作第三个连接这些我怎么能这样做? 我试过了: char three[400]; strcpy(three, one); strcat(three, two); 但这似乎不起作用。 如果one和two设置如下: char *one = “data”; char *two = “more data”; 任何人都知道如何解决这个问题? 谢谢

这里代码中的#define指令中的##是什么意思

请解释一下答案: #define f(g,h) g##h main(){ printf(“%d”,f(100,10)); }

在##连接之前评估预处理程序令牌

我想在它与其他东西连接之前评估一个令牌。 “问题”是标准将行为指定为 在重新检查替换列表以更换更多宏名称之前,删除替换列表中的##预处理标记的每个实例(不是来自参数),并将前面的预处理标记与以下预处理标记连接起来。 因此在下面的例子中, #include struct xy { int x; int y; }; struct something { char * s; void *ptr; int size; struct xy *xys; }; #define ARRAY_SIZE(a) ( sizeof(a) / sizeof((a)[0]) ) #define DECLARE_XY_BEGIN(prefix) \ struct xy prefix ## _xy_table[] = { #define XY(x, y) {x, y}, #define DECLARE_XY_END(prefix) \ {0, 0} \ […]