strcat pointer problem

This is a discussion on strcat pointer problem within the C Programming forums, part of the General Programming Boards category; How can i append a char pointer to a string? "abc" tempString now = abc" tempString now = abc"bc" tempString ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    strcat pointer problem

    How can i append a char pointer to a string?

    "abc"
    tempString now = abc"
    tempString now = abc"bc"
    tempString now = abc"bc"c"


    Code:
    char tempString[80];
    char *c;
    
    for (c = str_token; *c != '\0'; c++){
              if(*c == '"'){
                if(!append){
                  append = 1;
                }else{
                  append = 0;
                }
              }
              if(append && *c != '"'){
                strcat(tempString, c);           
              }
    
    }
    Last edited by erasm; 09-01-2009 at 06:46 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There's nothing wrong with your use of strcat; if there is some problem it has a different cause.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    strcat is designed to append a string to a string, not a char to a string.

    If you only want to move a character from *c to the end of tempString, you could use use a pointer for tempString, or use subscript notation, and a simple assignment, keeping track manually of the null terminator in tempString.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    I am now using a pointer for tempString, but getting a segmentation fault.

    Sorry Dino, i don't understand what you mean by assignment and manually keeping track of the null terminator.
    Code:
    char *tempString;
    char *c;
    
    for (c = str_token; *c != '\0'; c++){
              if(*c == '"'){
                if(!append){
                  append = 1;
                }else{
                  append = 0;
                }
              }
              if(append && *c != '"'){
                strcat(tempString, c);           
              }
    
    }
    Last edited by erasm; 09-01-2009 at 07:35 AM.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Your tempString pointer, and your c pointer, both point to nothing - neither has been initialized.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    Quote Originally Posted by Dino View Post
    Your tempString pointer, and your c pointer, both point to nothing - neither has been initialized.
    c is pointing to str_token though

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Try and describe more clearly what you are trying to do. As I said, there was nothing wrong with your use of strcat, or with either of the pointers in your first post, unless, as Dino implies, you only wanted to append one character at a time, in which case the result was not what you expected...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    I am just trying to append characters to the tempString for now.

    Desired output for "abc" would be
    tempString now = a
    tempString now = ab
    tempString now = abc
    Last edited by erasm; 09-01-2009 at 07:54 AM.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by erasm View Post
    I am just trying to append characters to the tempString for now.
    Okay, so think about how strtok() works. If you have this string:

    abc def

    and " " is the delimiter, the first token will be "abc" and the second "def". Those are both strings. If you append the second token to another string ("ghi") you will get "ghidef".

    If you only want to append a single character from "ghi", you need to use subscript[notation] and do something like this:
    Code:
    char def[16]="def", ghi[]="ghi";
    int len=strlen(def);
    def[len]=ghi[0];
    def[len+1]='\0'; /* null terminate! */
    *def will now be "defg". Notice I left enough space in def to append into. Make sure you scroll down and look at the last line. If you do this:
    Code:
    char def[16] = {0};
    strcpy(def,"def");
    you can save yourself the trouble of having to add a null terminator each time you add a character, since *def was initialized to be all '\0'.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    I'm having difficulty translating your method into my function. How would i iterate over a string's characters appending certain ones to tempString?

    Code:
      int process_token (char *token, int append) {
        for (c = token; *c != '\0'; c++){
          if(*c == '"'){
            if(!append){
              append = 1;
            }else{
              append = 0;
            }
          }
          if(append && *c != '"'){
            strcat(tempString, c);
            printf("tempString now = %s\n", tempString);
          }
        }
        printf("append = %d\n", append);
        return append;
      }
    Last edited by erasm; 09-01-2009 at 08:54 AM.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    There is no standard function to append characters to a string, so I suggest you write your own.

    Code:
    void char_cat(char* s, char c)
    {
        /* Append the character to the end of the string */
        char* p = s;
        while(*p != '\0')
            p++;
        /* We've found the end of the string.  I'll let you add the code to place the new char at the end */
        ...
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    Why is this code getting a segmentation fault?

    Code:
    void char_cat (char *s, char c) {
        printf("string to concatenate = %s\n", s);
        char *p = s;
        int len = strlen(s);
        printf("string length = %s\n", len);
        while(*p != '\0'){
          p++;
        }
    }
    
    int process_token (char *token, int append) {
    
        char *qString = token;
    
        for (c = token; *c != '\0'; c++){
          if(*c == '"'){
            if(!append){
              append = 1;
            }else{
              append = 0;
            }
          }
          if(append && *c != '"'){
            char_cat(qString, c);
            printf("qString now = %s\n", qString);
          }
        }
        printf("append = %d\n", append);
        return append;
    }
    Function call:

    Code:
    append = process_token (str_token, append);

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Is c a global variable? If so, why?

    You appear to be passing two char pointers to char_cat (both qString and c must be char * for the rest of the code to make sense), not a char pointer and a char.

    char_cat does not do anything, except walk off the end of memory if p is not a null-terminated string. (That's not really an issue with char_cat -- most of the standard library will do the same thing, and in fact if that's a problem strlen() will cause problems before you get to your code -- but you do need to be sure about what you pass to it.)

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bithub View Post
    [code]
    /* We've found the end of the string. I'll let you add the code to place the new char at the end */
    So for example this might be the stuff from post #9, except c would replace ghi[x].
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    void char_cat (char *s, char c)
    don't you have a warning that parameter c is not used in this function?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 03:50 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM

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