Thread: string value disappearing after strcat

  1. #1
    Registered User
    Join Date
    Dec 2021
    Posts
    2

    string value disappearing after strcat

    Trying to add a slash to the end of a string that is pulled from an argument (linux). I read in the argument just fine, then add the slash and poof - no more variable. I trimmed it down to bare bones and it's still doing it. Here's the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( int argc, char *argv[] ) {
       int i;
       char * first_arg = argv[1];
       char * second_arg = argv[2];
       char * third_arg = argv[3];
       char fullcommandline[255] = "";
    
       for( i = 0 ; i < argc ; i++ ) {
          strcat(fullcommandline, argv[i]);
          strcat(fullcommandline, " ");
       }
       i = strlen(second_arg);
       printf("last character in second_arg is:     %c\n",second_arg[i-1]);
       if (second_arg[i-1] != '/') {
          strcat(second_arg, "/");  // Also have tried "/\0" here w/ same results
       }
       i = strlen(second_arg);
       printf("last character in second_arg is now: %c\n",second_arg[i-1]);
       printf("After change:\n");
       printf("\tCommand Line : %s\n",fullcommandline);
       printf("\tfirst_arg    : %s\n", first_arg);
       printf("\tsecond_arg   : %s\n", second_arg);
       printf("\tthird_arg    : %s\n\n", third_arg);
       return 0;
    }
    Here's the output:
    Code:
    user@localhost: ~$ ./test -something ./a_directory ./anotherdir
    last character in second_arg is:     y
    last character in second_arg is now: /
    After change:
        Command Line : ./test -something ./a_directory ./anotherdir 
        first_arg    : -something
        second_arg   : ./a_directory/
        third_arg    :
    thoughts?
    Thanks

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    822
    The argv arguments seem to be stored contiguously (this isn't a guarantee, but it's how it's done in this implementation):


    Code:
    arguments:  ./test\0-something\0./a_directory\0./anotherdir
    argv index: 0       1           2              3

    After you append a slash to argv[2], the null terminator is one character later, argv[3][0] is now that same null terminator (the first character was overwritten):

    Code:
    arguments:  ./test\0-something\0./a_directory/\0/anotherdir
    argv index: 0       1           2             3
    A string with a null terminator in the first character is an empty string.

  3. #3
    Registered User
    Join Date
    Dec 2021
    Posts
    2

    Lightbulb

    Quote Originally Posted by christop View Post
    The argv arguments seem to be stored contiguously (this isn't a guarantee, but it's how it's done in this implementation):
    Code:
    arguments:  ./test\0-something\0./a_directory\0./anotherdir
    argv index: 0       1           2              3
    The light bulb just snapped on, thanks! So I need to get that into another string (via strcpy) before I start walking over everything else. I tested it out and it works fine now.
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat causing string to become empty
    By ddd in forum C Programming
    Replies: 2
    Last Post: 04-16-2012, 01:41 PM
  2. String Concatenation Using Pointers No strcat()
    By oviang in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 10:31 AM
  3. strcat (), argv[], general string trouble
    By markjoe in forum C Programming
    Replies: 11
    Last Post: 10-30-2007, 04:53 PM
  4. Question on string concatenate (strcat)...
    By cjmdjm in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2005, 12:42 AM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-14-2001, 12:09 AM

Tags for this Thread