c - How to convert character from an array into a string -


i'm having difficulty appending character value of array onto string (handsorted). hand[] predefined array of text.

char *handsorted = malloc(strlen(hand)+1); strcat(handsorted, hand[2]); 

for example handsorted string of value of hand[2], letter 'a'.

when dealing c, learn how use manual pages in terminal. here entry strcat.

description  strcat() , strncat() functions append copy of   null-terminated string s2 end of null-terminated  string s1, add terminating `\0'.   

that's 1 problem. need handsorted null terminated.

char *handsorted = malloc(strlen(hand)+1); handsorted[0] = '\0'; strcat(handsorted, hand[2]); 

but there still problem. hand[2] single character, , strcat() expects character pointer, aka string. need pass address of character using 'address-of' operator - &. this.

char *handsorted = malloc(strlen(hand)+1); handsorted[0] = '\0'; strcat(handsorted, &hand[2]); 

i think we're after.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -