Hi everyone,
There are some problem with the pointers in my program:
what can i change in my code?Code:#include <stdlib.h> #include <string.h> int main(){ char *s1 = "Opa"; char *s2 = "hail!"; strcat(s1,s2); printf("%s", s1); return 0; }
Thanks.
Hi everyone,
There are some problem with the pointers in my program:
what can i change in my code?Code:#include <stdlib.h> #include <string.h> int main(){ char *s1 = "Opa"; char *s2 = "hail!"; strcat(s1,s2); printf("%s", s1); return 0; }
Thanks.
make s1 space for s1 and s2, and make it not point to a string constant. If you change a string constant ("osidjidfs") then your program could crash.
to do that, you could make s1 an array with space enough for both:http://www.cplusplus.com/reference/c...ng/strcat.htmlCode:char s1[20] = "Opa"; char *s2 = "hail!";
http://www.cprogramming.com/tutorial/c/lesson8.html
http://www.cprogramming.com/tutorial/c/lesson9.html
http://www.cprogramming.com/tutorial/c/lesson6.html
Last edited by robwhit; 08-15-2007 at 11:27 PM.
exactly as robwhit pointed s1 does not have enough memory to store s1+s2 and hence the problem.
Make s1 an array or allocate enough memory for s1 so that it may store the length of s1+s2.