This works:
const char * test = "Hello My\0friends\0I love food!";
const char * section1 = test;
const char * section2 = strchr(section1, 0) + 1;
const char * section3 = strchr(section2, 0) + 1;
Also, note that the section variables are just pointers into the test
string. That's fine if you are defining test
the way I did above, but if you are defining test
in some other more complicated way, you need to make sure the test
string does not get deallocated or modified, or you need to make copies of the section strings before that happens.
Don't forget to include string.h
at the top of your program.
However, I'd like to add that if you didn't know about the strchr
function, it should be trivial to implement it yourself by writing a loop that iterates through the string and looks for a null character and returns the pointer to it.