OK, I have managed to get rid of all the errors whilst compiling, but I am not getting the output I want from the function.
Code:
void printDescription(const char *description, const int maxLineWidth) {
char buff[maxLineWidth + 1], *b;
char word[maxLineWidth + 1], *w;
b = buff;
w = word;
for(; *description !='\0'; description++) {
*w = *description;
if (*description == ' ' && strlen(w) + strlen(b) <= maxLineWidth) {
b = strncat(b, w, strlen(w + 1));
*w = 0;
}
else if (*description == ' ' && strlen(w) + strlen(b) > maxLineWidth) {
printf("%s\n", b);
*b = 0;
b = w;
*w = 0;
}
else {
w++;
}
}
printf("%s\n", b);
}
From my analysis I think the problem is where I have *w=0. What I want to do here is effectively 'reset' the string so it is effectively empty, and the pointer is back at the starting position. I suspect what it is actually doing is just putting a 0 value at the current position and continuing on. Does anybody know what I would have to change it to to get the desired result?
The only other thing I thought it could be was if strlen included the null characters in the length of the string, but surely it doesn't...
Many thanks