[英]single char strcat -C
I want to put two single char
together.我想把两个单
char
放在一起。 Should I use the function strcat?我应该使用函数strcat吗?
the following is my code以下是我的代码
int main(){
char *a[4]={"1","2","3","4"};
char *new_a[2];
for (i=0;i<2;i++){
new_a[i]=strcat(a[2*i],a[2*i+1]);
printf("%c",*new_a[i]);
}
return 0;
}
I want to print new_a[0] to be 12, and new_a[1] to be 34我想将 new_a[0] 打印为 12,将 new_a[1] 打印为 34
Without being 100% sure what you need, here is some code that gives the output you suggest, by concatenating the first and the last two characters of your character array together.如果不能 100% 确定您需要什么,这里有一些代码可以通过将字符数组的第一个和最后两个字符连接在一起来给出您建议的输出。
Also note that what you have is not an array of characters, by an array of pointer, so you might want to have a look into it as well.另请注意,您所拥有的不是字符数组,而是指针数组,因此您可能还想查看它。
Here is the code:这是代码:
#include "stdio.h"
int main(void){
char a[4]={'1','2','3','4'};
char str[8] = {'{', a[0], a[1], ',', a[2], a[3], '}', '\0'};
printf("string: %s\n",str);
return 0;
}
Output:输出:
string: {12,34}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.