Thread: strnlen, implicit declaration?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    11
    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

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Code:
    = {0}
    first and you'll find that works better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    11
    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’)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM