Hello is it possible to add a char to another char like:
char *myChar = ("Hello ");
*myChar += ("World!");
printf("%c", *myChar);
Thank you!
Hello is it possible to add a char to another char like:
char *myChar = ("Hello ");
*myChar += ("World!");
printf("%c", *myChar);
Thank you!
不是这样的。您必须提供缓冲区并使用strcpy
并strcat
构建组合字符串。
如果要连接两个字符串,请使用库函数strcat
。
测试程序
#include <stdio.h>
#include <string.h>
int main (void)
{
char src[50], dest[50];
strcpy(src, "Hello ");
strcpy(dest, "World");
strcat(src, dest);
printf("Concatenated string is : |%s|", src);
return(0);
}
如果我从字面上理解你的问题是有可能的,那么你的例子是为了别的。
您可以添加 2 个字符,例如,
char a = 'a';
char b = 'b';
char result = a + b;
您可以添加 2 个 char 指针,这可能会给您一个无效的指针,除非您确保它没有超出范围。
char* str1 = "string1";
char* str2 = "string2";
char* result = str1 + str2;
如果使用文字,您必须有一个临时存储空间,因为文字始终是 const。这可能是你能得到的最接近的:
printf("%s%s","Hello ","World");