Thread: strcat problems

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    strcat problems

    Hey guys, I'm trying to get into C and C++ from PHP and Java. Obviously I may be biased towards the two that I find easier. However, I have run into several problems that are ruining my experience with this language.

    First is the terrible implementation of strings. Especially in C.

    QQ aside, I wanted to know how to make something like this work;

    Code:
    char szX = "1";
    char szY[12] = "gugugaga"
    strcat(szY, szX)
    //I want "gugugaga1"
    the problem here is that the compiler wont accept anything other than a constant char value.

    The code above is part of the while clause and as such, the value of x changes every time the loop runs.

    Another question would be

    Does the C compiler automatically place a "/0" at the end of y[12]?

    Assuming that I am new to this language and the nuances of pointers and such like have not been completely mastered. How can I join the two strings?

    TL;DR: How do I get the strcat to join to variable char strings? How do I do it at all?

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    char szX[] = "1";    // "1" == {'1', '\0'}

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I suggest you should go grab a good tutorial on C.
    Because you know java and php doesn't mean you would be able to just straightaway code in C.

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    If a value is supposed to change on each iteration of while-loop you can do:
    Code:
    char szX[2];
    int counter = 0;
    while ( condition )
    {
        szX[0] = itoa( counter );
        strcat( szY, szX );
        counter++;
    }
    
    // Or more involved...
    char date[12];
    int year, month, day;
    year = 2010;
    month = 1;
    day = 1;
    while ( month <= 12 )
    {
        while ( day <= 30 )
        {
            sprintf( date, "%02d-%02d-%4d\n", day, month, year );
            printf( "%s", date );
            day++;
        }
        month++;
        day = 1;
    }

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    No go. The compiler basically want a constant string at the second argument of strcat

    strcat( szY, "1");

    works

    strcat( szY, szX ) doesn't work.

    Is it that I have a bad compiler, or is it the quirky language?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It isn't a quirk or your compiler. It would not surprise me if strcat() arguments were restricted, which forces them to be different pointers.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by thundercraker View Post
    Hey guys, I'm trying to get into C and C++ from PHP and Java. Obviously I may be biased towards the two that I find easier. However, I have run into several problems that are ruining my experience with this language.

    First is the terrible implementation of strings. Especially in C.
    First off, C doesn't have strings, at all. None, zero, zip, nada

    Many of the C library functions treat arrays of characters as strings, but that is purely a construct of the libraries.

    QQ aside, I wanted to know how to make something like this work;

    Code:
    char szX = "1";
    char szY[12] = "gugugaga"
    strcat(szY, szX)
    //I want "gugugaga1"
    the problem here is that the compiler wont accept anything other than a constant char value.
    To add variable content to char arrays you need library functions, not the equals sign.

    If you want to add x on the the back end of y you first need to make sure that x has a sufficient buffer to hold both strings... if X is 10 chars and Y is 7, you need 17 characters for the text and one for the trailing null. The null is essential as it tells library functions when to stop reading.

    Code:
    char szX[50] = {"This is "};
    char szY[] = {"a test"};
    strcat (szX,szY); 
    puts (szX);
    Or similar.

    The C compiler will not allocate the memory for you. As you get further into the language you will discover this is actually an advantage because it lets you control how, where and when a string buffer is created.

    The code above is part of the while clause and as such, the value of x changes every time the loop runs.
    If that's the case you need to use library functions, not initializers to get the task done... Look into functions such as strcpy(), memcpy() and wcscpy() for more on this.

    Another question would be

    Does the C compiler automatically place a "/0" at the end of y[12]?
    No, the compiler does not. The various string handling functions in the libraries do.


    Assuming that I am new to this language and the nuances of pointers and such like have not been completely mastered. How can I join the two strings?

    TL;DR: How do I get the strcat to join to variable char strings? How do I do it at all?
    Actually it's very easy... my example (poor as it is) should point you in the right direction. But you would be a lot further ahead if you stopped thinking "strings" and started thinking "character arrays" ...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteflags
    It isn't a quirk or your compiler. It would not surprise me if strcat() arguments were restricted, which forces them to be different pointers.
    Indeed. One way strcat might be written is to first find the end of the first string, and then traverse over the second string until its end to append to the first string, character by character. If the pointers initially point to the first character of the same string, this would then result in an infinite loop, since the second string will grow as fast as the first string.

    Quote Originally Posted by CommonTater
    First off, C doesn't have strings, at all. None, zero, zip, nada

    Many of the C library functions treat arrays of characters as strings, but that is purely a construct of the libraries.
    No. Standard C does not have a string type, but it does have a concept of a string, inherent in the special treatment for string literals as well as certain standard library functions. It is important to recognise the convention of a terminating null character in this string concept, otherwise one could fail to actually have a string.

    Quote Originally Posted by CommonTater
    To add variable content to char arrays you need library functions, not the equals sign.
    Not entirely true, since one can always do the work directly without using library functions. This is actually correct:
    Code:
    char szY[12] = "gugugaga";
    though this would have been enough:
    Code:
    char szY[] = "gugugaga";
    But your examples are wrong:
    Code:
    char szX[50] = {"This is "};
    char szY[] = {"a test"};
    They should have been:
    Code:
    char szX[50] = "This is ";
    char szY[] = "a test";
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Thanks. Owait.

    So let me put the question this way.

    strcat wants a constant char value in its second argument right? (I know this much is right)

    szX[] ="1" isnt a constant string is it? So how does this get passed at all?

    I mean if I did

    int cnt=0;
    szX[] = "3242342341";
    szY[100] = "gaga"
    while (cnt<10) {
    strcat( szY, szX[cnt] );
    cnt++;
    }

    it wouldnt work. How do i make the above work?
    Last edited by thundercraker; 10-15-2010 at 02:16 AM.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    That is my quandary atm

  11. #11
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by CommonTater View Post
    Another question would be

    Does the C compiler automatically place a "/0" at the end of y[12]?
    No, the compiler does not. The various string handling functions in the libraries do.
    Yes, it does when you initialize a char array to a string literal, and provided that there is enough space in the array.
    Code:
    char str_a[5] = "foobar";    // {'f', 'o', 'o', 'b', 'a'} and a warning from a decent compiler
    char str_b[] = "foobar";      // {'f', 'o', 'o', 'b', 'a', 'r', '\0'};

  12. #12
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by thundercraker View Post
    How do i make the above work?
    You use code tags and sprintf().

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thundercraker
    strcat wants a constant char value in its second argument right?
    No. The second argument to strcat should be a pointer to a const char that is part of a string. Consider:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char first[12] = "hello ";
        char second[] = "world";
        strcat(first, second);
        printf("%s\n", first);
        return 0;
    }
    Note that second is not an array of const char, but it can still be converted to a pointer to const char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Ah I think my problem is with the fact that I am using a changing value for the second argument, like

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char first[12] = "hello ";
        char second[] = "world";
        for(int i; i<6; i++) 
           strcat(first, second[i]);
        printf("%s\n", first);
        return 0;
    }
    trying this gives me "invalid conversion from char to const char"

  15. #15
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Okay. What do you think does expression second[i] evaluate to?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-30-2009, 08:55 AM
  2. strcat() problems, maybe
    By cloudsword in forum C Programming
    Replies: 10
    Last Post: 09-13-2009, 02:52 AM
  3. strcat_s problems
    By Aisthesis in forum C++ Programming
    Replies: 18
    Last Post: 06-02-2009, 09:59 AM
  4. strcat problems
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2002, 06:51 AM
  5. Strcat function problems
    By s1k3 in forum C++ Programming
    Replies: 1
    Last Post: 12-14-2001, 12:20 AM