简体   繁体   中英

convert group of character to string in c

I AM receiving series of characters, take it as 10 number of characters from rs232. the received in a variable are in the form of characters or a group of characters. I want to make them string

Remember C does not have strings, but you can have an array of char s.

Create an array of chars of the appropriate size (one extra for \\0 ).

char str[11];

Loop over your characters, setting each one to the appropriate value in this char array.

for (int i = 0; i < 10; i++) {
    str[i] = your_chars[i];
}

Toss the null terminator on the end.

str[10] = '\0';

CodePad .

Fasked also points out that this can be achieved with strncpy() .

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