trouble understanding a problem when using strcat

Discussion in 'C Programming' started by sail0r@creepjoint.net, May 7, 2007.

  1. Guest

    Perhaps this is obvious but I am not sure what is going on...
    Here is the relevant code:
    char *command;
    char *argument;
    char url[]="file:///usr/u/myname/Project/cats/";
    char target_path[]="/tmp/abc";

    command=strtok(buf,":\n\r");
    argument=strtok(NULL,"\n\r");

    /*now, I want to strcat argument onto both url and target_path */
    strcat(url,argument);
    strcat(target_path,argument);

    But that doesn't work as I expect it to. Why?
    What I am seeing is that the first strcat work but the second one
    has a result string that is all discombobulated somehow?

    Any advice would be greatly appreciated!
     
    , May 7, 2007
    #1
    1. Advertising

  2. said:

    > Perhaps this is obvious but I am not sure what is going on...
    > Here is the relevant code:
    > char *command;
    > char *argument;
    > char url[]="file:///usr/u/myname/Project/cats/";


    How much storage is reserved for url[]? Count the bytes in the
    initialiser to find out. (Add 1 for the terminator.) Call this X.

    How much of this storage is used? Call this Y. What do you notice about
    X and Y? [1]

    > char target_path[]="/tmp/abc";
    >
    > command=strtok(buf,":\n\r");
    > argument=strtok(NULL,"\n\r");
    >
    > /*now, I want to strcat argument onto both url and target_path */
    > strcat(url,argument);


    Let us assume that argument now points to a string longer than zero
    bytes. Z, say, not including the terminator.

    How much storage will the url[] array require if it is to store both the
    information it already contains and the Z bytes of information you are
    now trying to add to it? [2]

    How much storage does it actually have? [3]

    How short are you of the necessary bytes? [4]

    Consider using dynamic memory allocation instead.


    Answers:

    [1] They are equal
    [2] Y + Z
    [3] Y
    [4] Z

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, May 7, 2007
    #2
    1. Advertising

  3. On 6 May 2007 22:54:40 -0700, wrote:
    >But that doesn't work as I expect it to. Why?


    Unlike other languages C has no string type. Really. You need to use
    '\0' terminated char arrays and provide the necessary automatic or
    dynamic memory space for them.


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
     
    Roland Pibinger, May 7, 2007
    #3
  4. pete Guest

    wrote:
    >
    > Perhaps this is obvious but I am not sure what is going on...
    > Here is the relevant code:
    > char *command;
    > char *argument;
    > char url[]="file:///usr/u/myname/Project/cats/";
    > char target_path[]="/tmp/abc";
    >
    > command=strtok(buf,":\n\r");
    > argument=strtok(NULL,"\n\r");
    >
    > /*now, I want to strcat argument onto both url and target_path */
    > strcat(url,argument);
    > strcat(target_path,argument);
    >
    > But that doesn't work as I expect it to. Why?
    > What I am seeing is that the first strcat work but the second one
    > has a result string that is all discombobulated somehow?
    >
    > Any advice would be greatly appreciated!


    /* BEGIN new.c */

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char *command = NULL;
    char argument[] = "\nWhat is \"buf\" supposed to be?\n\n";
    char url[sizeof "file:///usr/u/myname/Project/cats/"
    + sizeof argument]
    = "file:///usr/u/myname/Project/cats/";
    char target_path[sizeof url + sizeof "/tmp/abc"] = "/tmp/abc";
    /*
    ** command = strtok(buf, ":\n\r");
    ** argument= strtok(NULL, "\n\r");
    */
    /*now, I want to strcat argument onto both url and target_path */
    strcat(url, argument);
    strcat(target_path, argument);
    puts(url);
    puts(target_path);
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, May 7, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nicholas

    Using strcat

    Nicholas, Sep 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    869
    Jirka Klaue
    Sep 12, 2003
  2. Ian Stanley

    strcat problem

    Ian Stanley, Sep 12, 2003, in forum: C Programming
    Replies:
    5
    Views:
    3,622
    Irrwahn Grausewitz
    Sep 13, 2003
  3. Ian Stanley

    strcat problem again

    Ian Stanley, Sep 17, 2003, in forum: C Programming
    Replies:
    18
    Views:
    702
    Martijn Lievaart
    Sep 22, 2003
  4. JC

    strcpy and strcat problem

    JC, Sep 26, 2003, in forum: C Programming
    Replies:
    23
    Views:
    1,930
    Robert B. Clark
    Sep 29, 2003
  5. Replies:
    12
    Views:
    1,321
Loading...

Share This Page