#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char* a = "abcde";
char* b="";
strcat(b, a[1]); //想把a中的第一个字母a加到b中
printf("%s\n", b);
return 0;
}
两个错误,b是常量指针,不能修改内容;二是strcat是字符串连接,不是字符
你这么写,a和b都是常量指针,内容无法修改,参考如下:
#include <stdio.h>
#include <string.h>
int main()
{
char a[] = "abcde";
char b[10]= "efg"; //这里给b足够多的内存空间,10可以是任意大于4的数,因为efg占了3个,\0占1个
int n;
scanf("%d",&n); //将a的第n个字符插入b中
if(n >0 && n<strlen(a))
b[strlen(b)] = a[n-1];
printf("%s",b);
return 0;
}
如果b是指向字符串的字符指针的变量,那我觉得不可能将a中的字符追加到b中去。因为b指向的是一个字符串常量,这就意味这它不能被修改。如果 b是一个字符数组就可以。