LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-19-2010, 09:01 PM   #1
marquisor
Member
 
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?

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];
	length=strlen(text);
	for (i=0; i<=length; i++){
		if (text[i] == '.' || text[i] == ',' || text[i] == '!' || text[i] == ':' || text[i] == ';' || text[i] == '?' || text[i] == ' ')
			continue;
		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");
	gets(input);

	strcpy(new_string,terminate_symbols(input));

	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.
Regards

marquisor
 
Old 10-19-2010, 11:02 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

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

Second, you're misusing strcat. From the strcat man page:
Code:
SYNOPSIS
       #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-19-2010 at 11:06 PM.
 
Old 10-19-2010, 11:07 PM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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!
 
  


Reply

Tags
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 06:13 AM
[SOLVED] Why is char* a string and not a pointer to a char? el_b Programming 2 09-25-2009 10:33 AM
How to return empty char * ? cpthk Programming 16 08-28-2009 11:17 PM
strcat usage with char* and char [] fjkum Programming 7 12-13-2008 11:12 PM
Do Perl functions usually modify variables, or return modified copies? Why? johnMG Programming 3 02-06-2005 10:22 PM


All times are GMT -5. The time now is 11:01 AM.

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