Get a substring of a char*

user502230 picture user502230 · Nov 18, 2010 · Viewed 580.3k times · Source

For example, I have this

char *buff = "this is a test string";

and want to get "test". How can I do that?

Answer

Goz picture Goz · Nov 18, 2010
char subbuff[5];
memcpy( subbuff, &buff[10], 4 );
subbuff[4] = '\0';

Job done :)