strcat causing string to become empty

This is a discussion on strcat causing string to become empty within the C Programming forums, part of the General Programming Boards category; I have the following code where strcat is causing problem. Code: char* tokens = strtok(buf, "+"); int n = 0; ...

  1. #1
    ddd
    ddd is offline
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    strcat causing string to become empty

    I have the following code where strcat is causing problem.
    Code:
          char* tokens = strtok(buf, "+");
          int n = 0;
          int type = 0;
          char* name = "";
          char* lifetime = "";
          char* data = "";
          for(n=0; tokens!=NULL; n++) {
              if(n==0)
                type = atoi(tokens);
              if(n==1)
                name = tokens;
              if(n == 2) {
                 if(type == 1)
                    lifetime = tokens;
                 else
                    data = tokens;
              }
              if(n == 3)
                 lifetime = tokens;
              tokens = strtok(NULL, "+");
          }
    
    
          if(type == 2) {
             printf("Received Data with Name: %s, Data: \"%s\" and lifetime: %s seconds\n", name, data, lifetime);
    
             strncat(name, "+", 1);
             printf("Data: %s\n", data);
             strncat(name, data, strlen(data));
             printf("Full Name: %s\n", name);
          }

    In the line where I print the name, data and lifetime everything prints correctly. But after the strcat operations I find that "data" value is empty. After adding debug printf statements I have found that "data" value becomes empty right after
    Code:
    strncat(name,"+",1)
    What could be the reason? Please help.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    char* name = "";
    this makes name a string literal. you cannot append anything to that.
    try a char array.
    kurt

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    C strings are static arrays of chars, they don't grow or shrink dynamically. Your variable name, is just a pointer you can not use strcat to add characters to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String.empty v.s. string.null
    By George2 in forum C# Programming
    Replies: 4
    Last Post: 06-19-2008, 09:22 AM
  2. Flush / Empty a string
    By Buzzer in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2006, 02:36 PM
  3. strcat causing segmentation fault?
    By jbsloan in forum C Programming
    Replies: 2
    Last Post: 04-02-2005, 10:42 AM
  4. Tell if a string is empty?
    By dizz in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2002, 01:37 AM
  5. How to empty a string?
    By Zwen in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2002, 08:14 PM

Tags for this Thread


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