简体   繁体   中英

single char strcat -C

I want to put two single char together. Should I use the function 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

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.

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM