result:Code:char *p1; char *p2; p1 = "tttttttttt"; p2 = "sssssssss"; char *p3; p3 = strcat(p1,p2); printf("%s\n",*p3);
error !
why ?
How to solve this problem?
result:Code:char *p1; char *p2; p1 = "tttttttttt"; p2 = "sssssssss"; char *p3; p3 = strcat(p1,p2); printf("%s\n",*p3);
error !
why ?
How to solve this problem?
Last edited by zcrself; 08-13-2009 at 09:14 PM.
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.
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); //}
I was going through this thread and did this code:
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.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); }
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
Take a look at this snippet:Originally Posted by BEN10
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.Code:p1=malloc(50); p2=malloc(50); p1="my name is ben10"; p2="i like cboard and c";
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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:Originally Posted by BEN10
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; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I tried you example and it worked properly but I still dont understand why this is not working?
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.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; }
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
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).Originally Posted by BEN10
Put it another way. It is like writing this code:
then asking why the value of n is no longer 10 after the second line.Code:int n = 10; n = 123;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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