C - strcat and string/char functions, return variables
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The second parameter (src) is a const char * -- not a char. You can't change how strcat works by typecasting one of its arguments.
EDIT: And incidentally, your typecast doesn't do anything because text[i] is already a char. I'm guessing you got a warning from the compiler and you thought that an explicit typecast might make the warning go away,but you didn't take the time to figure out what the warning was trying to tell you.
Last edited by Dark_Helmet; 10-19-2010 at 11:06 PM.
1. The string pointer newtext being returned by terminate_symbols has been declared as a local variable in the function. This means that the string is no longer in scope (typically it might be created on the stack, so the old space might be overwritten by subsequent calls, including the call to the strcpy). You may find that it works in your example, but the behaviour is not defined, so it is a recipe for disaster.
You can fix this by passing in a pointer to new_string (which will also save having an extra strcpy after the function returns).
2. The newtext buffer was not initialized, so calling strcat will have undefined behaviour (since the end of the string is defined by a zero byte). Again, it might work through luck, but don't rely on it.
3. The function strcat copies the entire string that it is given, not just a single character. It requires a pointer to a string, not a single character (your cast is covering this up, and is the reason for the segmentation fault).
You would probably be better off copying the character explicitly, rather than using a library function (assuming this is a teaching exercise).
4. Functions such as gets and strcat are inherently unsafe, because they have no limit placed on how many characters they copy. This leads to buffer overflows (since C does not have automatic array index checking). It is worth learning alternatives such as fgets and strncat early on before you get bitten by them!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.