-
Code:
if (*description == ' ' && strlen(w) + strlen(b) <= maxLineWidth) {
else if (*description == ' ' && strlen(w) + strlen(b) > maxLineWidth) {
Both of these are undefined behaviour.
The reason is because ou never put anything in either buff or word (btw, your pointer variable names are lacking). You only assign one char. That doesn't add any NULL char, so it can go into infinity trying to find a NULL char.
Initialize your buffers to 0 with first and you'll find that works better.
-
Yes, sorry about the pointer names, I can be rather unthoughtful when it comes to naming some things. So you mean initialize them like this?
Code:
char buff[maxLineWidth + 1] = {0}, *b;
char word[maxLineWidth + 1] = {0}, *w;
When I do this I get the following when I try to compile it:
misc.c: In function ‘printDescription’:
misc.c:12: error: variable-sized object may not be initialized
misc.c:12: warning: excess elements in array initializer
misc.c:12: warning: (near initialization for ‘buff’)
misc.c:13: error: variable-sized object may not be initialized
misc.c:13: warning: excess elements in array initializer
misc.c:13: warning: (near initialization for ‘word’)
-
As the error says, you cannot initialize them to 0, because they variable-sized.
Better to use memset in that case, or set description + 1 to 0 (beware so that you don't do a buffer overrun).