Thread: How does the function strcat use?

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

    How does the function strcat use?

    Code:
     
        char *p1;
        char *p2;
        p1 = "tttttttttt";
        p2 = "sssssssss";
        
        char *p3;
        p3 = strcat(p1,p2);
        
        printf("%s\n",*p3);
    result:
    error !

    why ?
    How to solve this problem?
    Last edited by zcrself; 08-13-2009 at 09:14 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    p1 points to static memory which (1) can't be changed and (2) even if you could, would not have enough room to store more letters. Presumably you either want to (a) use arrays or (b) use malloc et al.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by zcrself View Post
    Code:
     
        p3 = strcat(p1,p2);
    result:
    error !

    why ?
    How to solve this problem?
    What you've attempted here is actually quite reasonable -- it's just not the way strcat() actually works. If strcat() allocated a NEW string, concatenated p1 and p2 together as this new string, and returned it, your code would be fine.

    However what strcat() actually does is append p2 directly to the end of p1, thereby ALTERING p1. Problem is, you can't alter p1 for the reasons tabstop mentioned: 1) the string it read-only and 2) it's not big enough even if you COULD write to it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by tabstop View Post
    p1 points to static memory which (1) can't be changed and (2) even if you could, would not have enough room to store more letters. Presumably you either want to (a) use arrays or (b) use malloc et al.
    how to solve?
    [code]
    char *p = malloc(100);
    Last edited by zcrself; 08-13-2009 at 09:39 PM.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I was going through this thread and did this code:
    Code:
    #include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int main()
    {
    	char *p1,*p2;
    	p1=malloc(50);
    	p2=malloc(50);
    	p1="my name is ben10";
    	p2="i like cboard and c";
    	puts(p1);
    	puts(p2);
    	strcpy(p1,p2);
    	printf("%s	%s",p1,p2);
    }
    I'm completely frustrated and in shock that why the above code is giving segfault even if I've malloced p1 and p2. Please help here as this will be helpful for the OP as well as me too. I think I'm missing something very minor.

    EDIT: If I also not initialize p1, then this program works fine. Is mallocing p1 doesn't make it writable?
    Last edited by BEN10; 08-14-2009 at 12:18 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BEN10
    I'm completely frustrated and in shock that why the above code is giving segfault even if I've malloced p1 and p2.
    Take a look at this snippet:
    Code:
    p1=malloc(50);
    p2=malloc(50);
    p1="my name is ben10";
    p2="i like cboard and c";
    From the first two lines, we see that p1 and p2 point to dynamically allocated memory (or to null pointers if malloc fails to allocate). In the next two lines, we see that p1 and p2 now point to the first character of string literals. Clearly, this is a problem: you have a memory leak, and your intention to avoid an attempt to change a string literal with strcat() is thwarted. A correct solution would be to use strcpy() to copy over the string literals.
    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

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by BEN10 View Post
    I was going through this thread and did this code:
    Code:
    #include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int main()
    {
    	char *p1,*p2;
    	p1=malloc(50);
    	p2=malloc(50);
    	p1="my name is ben10";
    	p2="i like cboard and c";
    	puts(p1);
    	puts(p2);
    	strcpy(p1,p2);
    	printf("%s	%s",p1,p2);
    }
    I'm completely frustrated and in shock that why the above code is giving segfault even if I've malloced p1 and p2. Please help here as this will be helpful for the OP as well as me too. I think I'm missing something very minor.

    EDIT: If I also not initialize p1, then this program works fine. Is mallocing p1 doesn't make it writable?
    occur error!
    why ?
    Last edited by zcrself; 08-14-2009 at 12:33 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BEN10
    If I also not initialize p1, then this program works fine. Is mallocing p1 doesn't make it writable?
    As I noted, it is the later assignment of "my name is ben10" to p1 that is the problem. I also notice that you did not actually demonstrate strcat(), but rather strcpy(). A more correct, though not very useful example would be:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
        const char part1[] = "my name is ben10";
        const char part2[] = ", i like cboard and c";
        char *str = malloc(sizeof(part1) + sizeof(part2) - 1);
        if (str)
        {
            strcpy(str, part1);
            puts(str);
            strcat(str, part2);
            puts(str);
            free(str);
        }
        return 0;
    }
    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
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I tried you example and it worked properly but I still dont understand why this is not working?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
        const char part1[] = "my name is ben10";
        const char part2[] = ", i like cboard and c";
        char *str = malloc(sizeof(part1) + sizeof(part2) - 1);
    	str="why is this wrong";//adding this line results in segf
        if (str)
        {
            strcpy(str, part1);
            puts(str);
            strcat(str, part2);
            puts(str);
            free(str);
        }
        return 0;
    }
    Please explain me in layman jargon. My main problem is that after malloc'ing str why is that it isn't behaving like an ordinary string.
    EDIT: The above code doesn't work but this one works fine. If str after mallocing is still read only then why this works.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
        const char part1[] = "my name is ben10";
        const char part2[] = ", i like cboard and c";
        char *str = malloc(sizeof(part1) + sizeof(part2) - 1);
        if (str)
        {
            strcpy(str, part1);
            puts(str);
            strcat(str, part2);
            puts(str);
    		*str='g'; //changing str
    		puts(str);
            free(str);
        }
        return 0;
    }
    Last edited by BEN10; 08-14-2009 at 12:46 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BEN10
    My main problem is that after malloc'ing str why is that it isn't behaving like an ordinary string.
    You basically discarded the effects of the malloc. The pointer no longer points to what was returned by malloc; it points to the first character of the string literal. It is as if the malloc was simply removed (but it has not, thus the memory allocated by malloc remains allocated, though unusable).

    Put it another way. It is like writing this code:
    Code:
    int n = 10;
    n = 123;
    then asking why the value of n is no longer 10 after the second line.
    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

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by laserlight View Post
    You basically discarded the effects of the malloc. The pointer no longer points to what was returned by malloc; it points to the first character of the string literal. It is as if the malloc was simply removed (but it has not, thus the memory allocated by malloc remains allocated, though unusable).

    Put it another way. It is like writing this code:
    Code:
    int n = 10;
    n = 123;
    then asking why the value of n is no longer 10 after the second line.
    Thanks laserlight. Thanks, Thanks, Thanks......................Now I've understood it. Please give me some part of your sharp brain, fools like me need it.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM