LinuxQuestions.org
Help answer threads with 0 replies.
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-16-2012, 07:57 AM   #1
Hamed
LQ Newbie
 
Registered: Sep 2010
Posts: 27

Rep: Reputation: 4
Question Amazing Output


In the name of Allah

Hello,everyone
I wrote the following program but the output of it is "hello hello" not
"hello hi". I know if I use strdup, this problem will be solved but I don't want to use this function because I have to free the memory.
Please, help me how to solve this problem without using strdup function

Thanks in advance

Quote:
#include <stdio.h>

#include <string>
using std::string;

const char* func1()
{
string str = "hello";

return str.c_str();
}

const char* func2()
{
string str = "hi";

return str.c_str();
}

int main()
{
char s[20];

sprintf( s, "%s\t%s\n", func1(), func2());
printf("%s\n",s);

return 0;
}
~
~
 
Old 10-16-2012, 08:15 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
The problem is that the object 'str' is deleted each time you get to the end of func1 or func2. You need to do something like:

Code:
#include <stdio.h>

#include <string>
using std::string;

string str = "hello";
string str2 = "hi";
const char* func1()
{
	return str.c_str();
}

const char* func2()
{

	return str2.c_str();
}

int main()
{
	char s[20];

	sprintf( s, "%s\t%s\n", func1(), func2());
	printf("%s\n",s);

	return 0;
}
but then func1 and func2 become unnecessary (you can just replace them with calls to str.c_str() and str2.c_str() respectively). So the answer kinda depends on what you're actually trying to do - different workarounds will be appropriate depending on the situation.

Last edited by Snark1994; 10-16-2012 at 08:17 AM.
 
1 members found this post helpful.
Old 10-17-2012, 12:41 AM   #3
Hamed
LQ Newbie
 
Registered: Sep 2010
Posts: 27

Original Poster
Rep: Reputation: 4
In the name of Allah

Thanks for your reply but I can't use your solution because this solution is useless when we have many functions with separate, this solution can't help.
Please, suppose another approach

Thanks in advance
 
Old 10-17-2012, 03:14 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,608

Rep: Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105
May Buddha bless us all

Please state what you really want to do.
 
  


Reply


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
The Amazing Database I Can't Use... Can I? supes Linux - Software 6 06-30-2004 11:53 AM
SpamAssassin 2.6 -- truly amazing dcexplorer Linux - Software 1 11-26-2003 03:13 AM
Linux = Amazing ooagentbender LinuxQuestions.org Member Success Stories 2 11-13-2003 09:02 AM
Slack (9.0) is truly amazing! PapaNoHair Slackware 26 05-07-2003 05:22 PM
amazing! CtrlAltDel Linux - Distributions 8 10-03-2002 07:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration