Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-19-2010, 10:01 PM   #1
Registered: Sep 2010
Location: Germany
Distribution: Debian
Posts: 53

Rep: Reputation: 3
Question C - strcat and string/char functions, return variables

What'swrong with this little code?

 * aufg36.c
 *  Created on: 20.10.2010
 *      Author: marquisor

												/* Includes */

#include <stdio.h>
#include <string.h>

												/* Funktionsprototypen */

char * terminate_symbols(char [80]);

												/* Funktionen */

char * terminate_symbols(char text[80]){
	int i,length;
	char newtext[80];
	for (i=0; i<=length; i++){
		if (text[i] == '.' || text[i] == ',' || text[i] == '!' || text[i] == ':' || text[i] == ';' || text[i] == '?' || text[i] == ' ')
		strcat(newtext,(char) text[i]);
	return newtext;

												/* Hauptprogramm */

int main(){
	char input[80];
	char new_string[80];

	printf("\n\tSatz-Terminator 1\n\n");
	printf("\nBitte einen beliebigen Satz eingeben. Maximale Lšnge 80 Zeichen!\n\n");


	printf("\nDer neue Text lautet: \n");
	printf(" %s",new_string);

	return 0;
Purpose: remove all symbols from text and print it out.

I don't know, something seems to be wrong with the "terminate_symbols" function, but i can't get it.

Thx in advance.

Old 10-20-2010, 12:02 AM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
First, you never initialize the variable newtext.

Second, you're misusing strcat. From the strcat man page:
       #include <string.h>

       char *strcat(char *dest, const char *src);
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-20-2010 at 12:06 AM.
Old 10-20-2010, 12:07 AM   #3
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Stretch (Fluxbox WM)
Posts: 1,389
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
There are several issues.

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!


coding, functions, return, strings, variables

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] return char* from func in c? kalleanka Programming 14 08-20-2010 07:13 AM
[SOLVED] Why is char* a string and not a pointer to a char? el_b Programming 2 09-25-2009 11:33 AM
How to return empty char * ? cpthk Programming 16 08-29-2009 12:17 AM
strcat usage with char* and char [] fjkum Programming 7 12-14-2008 12:12 AM
Do Perl functions usually modify variables, or return modified copies? Why? johnMG Programming 3 02-06-2005 11:22 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration