Thread: Parsing a string

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int parse ( char *input, char *output[], int maxtokens ) {
      char *seps = "|\n";
      int i;
      char *p;
      for ( i = 0, p = strtok(input,seps) ;
            i < maxtokens && p != NULL ;
            i++, p = strtok(NULL,seps) ) {
        output[i] = p;
      }
      return i;
    }
    
    int main ( ) {
      char input[] =  "command|MAC|field|field|field\n";
      char *output[10];
      int len = parse ( input, output, 10 );
      int i;
      for ( i = 0 ; i < len ; i++ ) {
        printf( "%s\n", output[i] );
      }
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    thanks!! this works.. tommorow I'll try to work this out! If I have any questions you'll hear from me again

  3. #18
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Dave_Sinkula
    )?You can't.
    function foo() below returns a 2d character array.
    Code:
    #include <stdio.h>
    char** foo()
    {
    	char **token;
    	token = malloc(8 * sizeof(char**));
    	token[ 0] = "002^Resultaat op aanmelden";
    	token[ 1] = "20050915";
    	token[ 2] = "121533";
    	token[ 3] = "INT^TEMP^1^S";
    	token[ 4] = "BOOL^LED^1^A";
    	token[ 5] = "BOOL^LED^2^A";
    	token[ 6] = "";
    	return token;
    }
    
    int main()
    {
    	char** tokens = foo();
    	char** ptr = tokens;
    	while(*ptr != 0)
    	{
    		printf("%s\n",*ptr);
    		ptr++;
    		fflush(stdout);
    	}
    	free(tokens);
    
    	return 0;
    }
    or like this
    Code:
    int main()
    {
    	int i;
    	char** tokens = foo();
    	for(i = 0; tokens[i] != 0; ++i)
    	{
    		printf("%s\n",tokens[i]);
    	}
    	free(tokens);
    
    	return 0;
    }

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Ancient Dragon
    function foo() below returns a 2d character array.
    No it doesn't. It returns a pointer to a pointer. A pointer to a pointer is not a two dimensional array. Perhaps you'd benifit from a FAQ. They're not the same thing. We often times use multiple pointers to simulate multidimensional arrays, but they're not the same thing. For one, with your "multi dimension array" that you're returning, it's perfectly legal to have it like this:
    Code:
    char **twodee;
    int x;
    
    twodee = malloc( ROWS * sizeof *twodee );
    for( x = 0; x < ROWS; x++ )
        twodee[ x ] = malloc( (rand() % 1000 + 1) * sizeof **twodee );
    This obviously can't be a two dimensional array, because each row can be any length I want it to be.

    Furthermore, your call to free is wrong in both examples. Rather, if you were doing anything other than assigning string literals, it would be.


    Quzah.
    Last edited by quzah; 10-19-2005 at 04:12 PM.
    Hope is the first step on the road to disappointment.

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you'll notice, I answered the question as asked.
    Quote Originally Posted by Dave Sinkula
    Quote Originally Posted by robin171
    how can i return a array of strings ??
    You can't.
    Whereas you did not.
    Quote Originally Posted by Ancient Dragon
    function foo() below returns a 2d character array.
    <snipped>
    No it does not. But I do know how these kinds of array simulation are done. I just don't know if it's particularly suited to what robin171 might need.
    Last edited by Dave_Sinkula; 10-19-2005 at 04:14 PM. Reason: Dammit! I am really getting pokey. :(
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #21
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by quzah

    Furthermore, your call to free is wrong in both examples. Rather, if you were doing anything other than assigning string literals, it would be.
    Quzah.
    The call to free is indeed correct in my example. It did only one malloc(), therefore it needs only one free().

    2d arrays can be declared in ways -- argv is a good example. It can be declared as "char **argv", or "char *argv[]" and both are identical. However you do it, it is a 2d array of strings.
    Last edited by Ancient Dragon; 10-20-2005 at 07:17 AM.

  7. #22
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Ancient Dragon
    argv is a good example. It can be declared as "char **argv", or "char *argv[]" and both are identical. However you do it, it is a 2d array of strings.
    Except argv isn't a 2d array of strings.
    the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #23
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    thnx for the replies, I'm working on the example of Salem and it works pretty good. Maybe I need some more help later, for now I can work with this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String parsing
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-03-2008, 05:06 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM