简体   繁体   中英

split a multi-line string in C

how can I split a multi-line string in const char* array.?

Here is the input string.

const char* str = "122.123,-38.789"
                  "122.123,-39.78";

I need to get two string from this.

str1 = "122.123,-38.789";

str2 = "122.123,-39.78";

output of

printf("string='%s'", str)

string='122.123,-38.789122.123,-39.78'

What should be done to split this string ?

Use an array of char *

#include <stdio.h>
const char* str[] = { "122.123,-38.789" ,  "122.123,-39.78" };

int main(){
  printf("%s\n%s\n",str[0],str[1] );
  return 0;
}

How the compiler understood your code:

The C pre-processor concats strings placed together,

const char* str = "122.123,-38.789"
                  "122.123,-39.78";

The linebreak between the strings is parsed as a blank, treated the same as a space or a tab. So it's equivalent to:

const char* str = "122.123,-38.789" "122.123,-39.78";

which the preprocessor converts to

const char* str = "122.123,-38.789122.123,-39.78";

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