LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 01-07-2006, 08:53 PM   #1
>minimalist<
LQ Newbie
 
Registered: Apr 2004
Distribution: Debian, Trustix 2.2, NetBSD 2.1
Posts: 21

Rep: Reputation: 15
string return


forgive me for my ignorance, but what is wrong with the following code?

Code:
#include <stdio.h>

int main()
{
char *str;
str = getstring();
printf("%s\n", str);
return 0;
}

char* getstring()
{
char *string;
strcpy(string,"text");
return string;
}
 
Old 01-07-2006, 09:00 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
The problem is simply that you didn't allocate any space for "string" - you just declared a pointer. Here's one possible solution:
Code:
#include <stdio.h>
#include <string.h>
#include <malloc.h>

char * getstring (char *buffer, int buflen);

int
main()
{
  char *s = malloc (10);
  printf("%s\n",
    getstring(s, 10));
  free (s);
  return 0;
}

char*
getstring(char *buffer, int buflen)
{
  strncpy(buffer,"text",buflen);
  return buffer;
}
You certainly don't need the "malloc()" - you could just as easily have declared "char s[10]" and accomplished the same thing (and eliminated the call to "free()" at the same time).

Similarly, strncpy() copy is arguably "safer" than "strcpy()" in preventing buffer overruns - but unfortunately, it, too, has its own problems.

Bottom line: allocate string space, and your program is fine!

'Hope that helps ..
 
Old 01-07-2006, 09:06 PM   #3
>minimalist<
LQ Newbie
 
Registered: Apr 2004
Distribution: Debian, Trustix 2.2, NetBSD 2.1
Posts: 21

Original Poster
Rep: Reputation: 15
Thank you, and yes, that helps quite a bit.
 
Old 01-08-2006, 04:49 AM   #4
vetrimani
LQ Newbie
 
Registered: Dec 2005
Posts: 8

Rep: Reputation: 0
#include <stdio.h>

int main()
{
char *str;
str = getstring(4);
printf("%s\n", str);
return 0;
}

char* getstring(int len)
{
char *string = malloc(len+1); /*One extra for null char at end*/
strcpy(string,"text");
return string;
}
 
  


Reply



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
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
can a function return a string? hubabuba Programming 13 03-06-2005 02:51 PM
String return question k1ll3r_x Programming 3 11-10-2004 02:46 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
string as return value in c raven Programming 6 02-10-2002 06:09 AM

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

All times are GMT -5. The time now is 03:48 PM.

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
Open Source Consulting | Domain Registration