Thread: pointing thru a char *

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    55

    pointing thru a char *

    The idea is to parse a specifically delimited string, and based on the delimiters, store the data between them into an array.

    Sounds confusing, but here's what I'm trying to do.

    I have a string. "0=up,1=down,2=side*"

    I would like to take parse out the "up", "down", "side", and stick them in the appropriate array index. So basically:

    puzzle[0] would contain "up"
    puzzle[1] would contain "down"
    puzzle[2] would contain "side"

    Okay... here's the code (that's not working).

    Code:
    void parseKeywords()
    {
    	char *orig = "0=up,1=down,2=side*";
    	char buf[1024];
    	char *stuff = NULL;
    
    	while (*orig && *orig != '*')
    	{
    		switch (*orig)
    		{
    		case '0':
    			*orig++; // Now pointing to "="
    			*orig++; // Now pointing to "u" in "up"
    
    			stuff = *orig;
    			
    			// Hopefully 'stuff' will contain the "u".
    			// Let's put that "u" into buf
    			snprintf(buf, sizeof(buf), "%s", stuff);
    
    			*orig++; // Now pointing to "p" in "up"
    
    			while (*orig && *orig != ',')
    			{
    				// cycle thru and build the buf.
    				strcat(buf, orig);
    				*orig++;
    			}
    			game[0].puzzle.keyword[0] = _strdup(buf); // buf should be "up"
    			break;
    		case '1':
    			*orig++; // Now pointing to "="
    			*orig++; // Now pointing to "d" in "down"
    
    			stuff = *orig;
    			snprintf(buf, sizeof(buf), "%s", stuff);
    			*orig++;
    
    			while (*orig && *orig != ',')
    			{
    				strcat(buf, orig);
    				*orig++;
    			}
    			game[1].puzzle.keyword[1] = _strdup(buf); // buf should be "down"
    			break;
    		case '2':
    			*orig++; // Now pointing to "="
    			*orig++; // Now pointing to "s" in "side"
    
    			stuff = *orig;
    			snprintf(buf, sizeof(buf), "%s", stuff);
    			*orig++;
    
    			while (*orig && *orig != ',')
    			{
    				strcat(buf, orig);
    				*orig++;
    			}
    			game[2].puzzle.keyword[2] = _strdup(buf); // buf should be "side"
    			break;
    		}
    		*orig++;
    	}
    }
    Any pointers (pardon the pun) would be appreciated.
    Last edited by xwielder; 02-07-2010 at 03:00 PM. Reason: minor mistake

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm curious as to what you think all these does:

    *orig++; // Now pointing to "="
    *orig++; // Now pointing to "u" in "up"

    stuff = *orig;

    Also, did you know that a C-string ends with a '\0'? You don't need the * at the end.
    Also, did you know of strtok?
    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
    Nov 2006
    Posts
    55
    Quote Originally Posted by Elysia View Post
    I'm curious as to what you think all these does:

    *orig++; // Now pointing to "="
    *orig++; // Now pointing to "u" in "up"

    stuff = *orig;

    Also, did you know that a C-string ends with a '\0'? You don't need the * at the end.
    Also, did you know of strtok?

    *orig++ moves the pointer to the next position in orig. Correct?

    And yes, I know that C-strings end with a '\0'. There are a few other reasons I choose to have a specific end of string delimiter, but thank you for reminding me again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by xwielder View Post
    *orig++ moves the pointer to the next position in orig. Correct?
    orig++ moves the pointer to the next position in orig.
    Why is there a * in there too?
    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.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    I could sit here all day and explain my thinking behind every line of code I've shown, and I can understand and appreciate that you may be trying to get me to look harder at it and discover my own mistakes, but I honestly don't have the time to play 100 questions. If you see my mistake, just say so by pointing it out with a brief explanation. If you don't know, that's fine too. Don't take my demeanor as being rude, because I mean no ill will at all. I'm quite thankful that someone is taking time to reply, however, a little more "to the point" would be nice.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because I am trying to understand your way of thinking. It would me lots of trouble trying to explain everything, since I cannot assume where your knowledge or thought process if failing you.
    Basically, the first one *orig++ is the same as
    *orig
    orig++
    So, as you see, the first has no effect whatsoever.
    stuff = *orig is very wrong because you would assign a char to a char pointer.
    You also seem to be under the impression that strcat copies a char. It does not. It copies a string. So you don't need a loop and try to copy over the string - 1 to the current start position every time.
    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.

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I am wondering why not use strtok() and be done with it?
    strtok - C++ Reference
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by xwielder View Post
    I could sit here all day and explain my thinking behind every line of code I've shown, and I can understand and appreciate that you may be trying to get me to look harder at it and discover my own mistakes, but I honestly don't have the time to play 100 questions. If you see my mistake, just say so by pointing it out with a brief explanation. If you don't know, that's fine too. Don't take my demeanor as being rude, because I mean no ill will at all. I'm quite thankful that someone is taking time to reply, however, a little more "to the point" would be nice.
    As far as this goes, I think you need to keep in mind that the forum here subscribes to the wisdom of "give a man a fish and he will eat for a day. Teach him to fish and he will eat for a lifetime". IOW in trying to understand why you are doing things the way that you are someone might be trying to fix the problem at the source: the programmer. Otherwise you will keep making the same mistakes and asking the same questions. And this is not meant to be rude either, just sharing lessons learned from a lifetime of coding...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #9
    Registered User
    Join Date
    Jan 2010
    Location
    Germany, Hannover
    Posts
    15

    Smile

    simply processing char by char
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void parseKeywords(char keyword[3][30]){
    	char *orig = "0=up,1=down,2=side*";
     char* kp=NULL;
     int pos=0;
    
     do{
        if (*orig=='=')
            kp=keyword[pos++]; //set pointer to next keyword, when starting char is reached
        else if (*orig==',' || *orig=="\0"){// terminating char reached? 
             if (kp)                     //keyword exists?(pointer set?)
                *kp=0,kp=NULL;       // ... then add string terminator to last keyword
        }
        else if (kp)
             *kp++=*orig; //copy char to "active" keyword
     } while(*orig++);  //move to next char,  until zero char was processed
    }
    
    int main(){
       char keyword[3][30];
       int i=0;
        parseKeywords(keyword);
        for(i=0;i<3;i++)
          printf("%s\n",keyword[i]);
        system("pause");
    }

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    @kermitaner : As you were posting your code, I was looking at strtok. Although I got it working, I will certainly try your code as well (for learning reasons).

    Here is the working code I've come up with. It may be a bit 'heavier' than I would like, but it works beautifully. A huge thanks to "Elysia" and "jeffcobb" for consistently mentioning the strtok function, but I suppose it was fear of my unknowing about the function that kept me from trying it.

    Anyway, here's what I have come up with:

    Code:
    void parseRoomPuzzleKeywords(int room, char *orig)
    {
    	while (*orig && *orig != '*')
    	{
    		char delims1[] = "=";
    		char delims2[] = ",";
    		char *result = NULL;
    
    		switch (*orig)
    		{
    		case '0':
    			orig++;
    			while (*orig && *orig == ',')
    				orig++;
    			char buf0[500];
    			snprintf(buf0, sizeof(buf0), "%s", orig);			
    			result = strtok( orig, delims1 );
    			result = strtok( result, delims2 );
    			orig = buf0;			
    			world[room].puzzle.actionKeyword[0] = _strdup(result);
    			break;
    		case '1':
    			orig++;
    			while (*orig && *orig == ',')
    				orig++;
    			char buf1[500];
    			snprintf(buf1, sizeof(buf1), "%s", orig);			
    			result = strtok( orig, delims1 );
    			result = strtok( result, delims2 );
    			orig = buf1;			
    			world[room].puzzle.actionKeyword[1] = _strdup(result);
    			break;
    		case '2':
    			orig++;
    			while (*orig && *orig == ',')
    				orig++;
    			char buf2[500];
    			snprintf(buf2, sizeof(buf2), "%s", orig);			
    			result = strtok( orig, delims1 );
    			result = strtok( result, delims2 );
    			orig = buf2;			
    			world[room].puzzle.actionKeyword[2] = _strdup(result);
    			break;
    		}
    		orig++;
    	}
    }
    Last edited by xwielder; 02-07-2010 at 05:30 PM.

  11. #11
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Here is the working code I've come up with. It may be a bit 'heavier' than I would like, but it works beautifully.
    I agree completely - it is very 'heavy' code. Mostly because of the repetition involved... and if you haven't done so already (it has been a few hours since the last post), this is a perfect place to make a function to reduce that repetition. Or better yet, use ASCII character set tricks to get rid of the enormous switch statement. Since the numbers in the character set are contiguous, you can turn a character into its represented digit like this:

    Code:
    char c = '5';
    char digit = c - '0'; //now digit = 5
    This should open up some new possibilities for streamlining your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM