I try to make a small function to get a string between two tags. But I get segfault on
str[len -3] = '\0';
format for a message
//returns the string beetween tags
char* GetMessage(char* str)
int len = strlen(str);
for(int i = 0 ; i < len;i++)
if(str[i] == '<' && str[i+1] == 'm' && str[i+2] == '>')
if(str[len-3] == '<' && str[len-2] == '/' && str[len-1] == '>')
str[len - 3] = '\0';
To resume answers for the questions.
1) Is it not possible to add a nulltermination to the string passed and then send a pointer back?
It turned out that the string passed as a literal was the problem in this case. Jonathan Leffler pointed this out in the comments and that was exactly how I tested the function.
When tested in conjunction with other functions answering without literals it worked well.
2) Is it bad practice to change the index of the pointer instead of copying it over to a buffer and send it back?
This appears to be of preference. But should not caus any problems.
3) I get a memory leak from the 3 bytes never getting freed?
This was really good explained here
Thanks for all the inputs!