Slight string problem

This is a discussion on Slight string problem within the C Programming forums, part of the General Programming Boards category; This is part of the code from a shell program I'm working on: Code: struct c { char command[10]; char ...

  1. #1
    Registered User DavidG's Avatar
    Join Date
    Mar 2006
    Location
    England
    Posts
    13

    Slight string problem

    This is part of the code from a shell program I'm working on:
    Code:
    struct c {
       char command[10];
       char argument1[40];
       char argument2[40];
    };
    
    
    
    struct c get_input(struct c cmd) /* Gets input from keyboard, and splits it up into command & arguments */
    {
       char input[92]; /* Full command string: command + any arguments */
    
       printf(">: ");                                     /* Print the prompt */
       gets(input);                                       /* Get input */
       cmd.command = strtok(input, " ");                  /* Get command */
       if ((cmd.argument1=strtok(NULL, " ")) == NULL)     /* Get first argument */
       {
          cmd.argument1 = "\0";                           /* if there isn't one, make null string */
       }
       if ((cmd.argument2=strtok(NULL, " ")) == NULL)     /* Get second argument */
       {
          cmd.argument2 = "\0";                           /* if there isn't one, make null string */
       }
       return cmd;
    }
    I'm getting 5 "incompatible types in assignment" errors in get_input(), one on each line with cmd.(whatever)
    As far as I can tell, it's complaining about me trying to put strings into the string variables in the 'cmd' structure, but I can't see why. Is there something I've overlooked? (I've included the structure defenition too, in case there's something wrong in there.)

  2. #2
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    Code:
     cmd.command = strtok(input, " ");
    What value ur returning from strtok(); ??? If ur returning "char *" then obviously u'll get such error,cause ur returning "char *" which is local in strtok() function.Instead pass cmd by reference.

  3. #3
    Registered User DavidG's Avatar
    Join Date
    Mar 2006
    Location
    England
    Posts
    13
    All I know is that strtok() is supposed to return a string... I suppose that'd be a char *, yeah.

    What do you mean by "pass cmd by reference"?

  4. #4
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    Pass address of cmd.command i mean string to function as follows
    something like

    Code:
     strtok(char *p,NULL, " ") { 
    /* char *p is pointer to cmd  */}
    & u call it as strtok(&cmd.command,NULL,"");

  5. #5
    Registered User DavidG's Avatar
    Join Date
    Mar 2006
    Location
    England
    Posts
    13
    Ah, I see what you mean. How do I give it the "input" string to split up? Just give it 'input' instead of 'NULL'?

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    cmd.command = strtok(input, " ");
    You can't do that.
    cmd.command is an array of characters. strtok() returns a char *.
    You need to use strcpy().
    Kurt

  7. #7
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    mU can think of modifying ur function strtok(input, " "); as
    Code:
     void strtok(input," ",char *dest) { 
     /* Ur code */
    }
    call it by
    Code:
    strtok(input," ",&cmd.command);
    strtok(input," ",&cmd.argument1);
    strtok(input," ",&cmd.argument2);
    Yes & indeed U've to use strcpy() or memcpy() to copy contents from input to char *dest

  8. #8
    Registered User DavidG's Avatar
    Join Date
    Mar 2006
    Location
    England
    Posts
    13
    Well, I didn't make the strtok() function myself, it's included in one of the C header files, so I have no idea how to make my own version.

    Would doing something like strcpy(cmd.command, strtok(input, " ")); work? Or is there some better way of splitting one string into several?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    Well the OP needs to read this
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Always use fgets() for reading lines from stdin or from a file.

    And vinit needs to read the man page for strtok() to see how many parameters it really has.

    > cmd.command = strtok(input, " "); /* Get command */
    Say
    Code:
    char *p = strtok(input," ");
    if ( p != NULL ) {
      if ( strlen(p) < sizeof cmd.command ) {
        strcpy( cmd.command, p );
      } else {
        // too long
      }
    } else {
      // nothing there
    }
    Your next use of strtok() is OK, in that you do pass NULL to indicate carry on with the current string, but you still need to assign the result to a char* variable, then check for NULL and then the length.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by DavidG
    Would doing something like strcpy(cmd.command, strtok(input, " ")); work? Or is there some better way of splitting one string into several?
    Yes that's the way to do it. Just be aware that strtok() returns 0 if it doesn't find any more tokens and I have never tried to call strcpy with a null-pointer as source pointer. Propably segfaults.
    Kurt
    EDIT: bit slow I am.

  11. #11
    Registered User DavidG's Avatar
    Join Date
    Mar 2006
    Location
    England
    Posts
    13
    Right, thanks. That's a great help, I've always had a bit of a blind spot with pointers. I'll try it out later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21