LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-07-2002, 06:51 AM   #1
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Rep: Reputation: 30
string as return value in c


hello

how do i have to declare the function header, so that it can return strings (ie arrays)

if i define a type with typedef:

typedef char string[40]

the function (string *)str=fgets(temp,10,file) doesnt compile because of incomptible types.

can you help me???

thank you

raven:smash:
 
Old 02-07-2002, 08:49 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
well, you're not using a string, you're using a char[], which are of course different things. i'm fairly sure c has NO string type, and if it does it'd only be a vague char[] wrapper. i know that the C++ String (capital S means it's a..) class is naturally not a primitive data type at any length.

you should simply return a pointer to a char, which you can then interpret as a string (in sematic terms, not datatype terms) as you wish
 
Old 02-07-2002, 08:58 AM   #3
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Original Poster
Rep: Reputation: 30
ok, thanks

i know thare is no such type inc as string or so, but if i declare it, i should be able to use it.

anyway, your idea is to return a pointer to a character. i thought of this, but what is the syntax??? i tryed randomly a few that seemed to me like OK, but (of course) it didnt work.

besides: what is the difference between char[] and *char?

in the memory both are (as far as i know) handled as pinters to a variable of the type char, then followed by more pointers. it is just another way to write it (isnt it?)

once i did this:

char array[5]={0,1,2,3,4};
and i had access to each field like this:
*(array+i)

it worked. thene where is the difference between char[] and *char???

thanks

raven
 
Old 02-07-2002, 10:15 AM   #4
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Code:
#include <stdio.h>

populateStr( char * pStr )
{
   strcpy( pStr, "Hello There" );
}

main()
{
   char szStrHolder[256];

   populateStr( szStrHolder );

   printf("String is [%s]\n", szStrHolder );

}
 
Old 02-07-2002, 01:20 PM   #5
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Original Poster
Rep: Reputation: 30
thanks, but that was not the problem.

i need to know how the header does have to look like if i RETURN the value from a function with

return(value);

the code is kind of this:

[some type] function(char *variable)
{
do something...
char *text;
text=malloc(sizeof(char)*10);
strcpy(text,variable);
return(*text);
}

what i need to know is what [some type] has to be? is it long or int, or some kind of other construct?

thanks anyway.

the problem with the solution you gave me (previous reply) is that i have this function in another .c file than the rest of the code. this function is called multiple times, and i thought it is better to make a separate function which returns a value than to declare variables multiple time with extern and so.

thanky you very much

raven
 
Old 02-07-2002, 03:46 PM   #6
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
This will be memory leak city if you do not clean up the memory aftwards.
That is why I like my previous example better, so long as you pass in the length of the string. (I did not do). You wouldn't need to do externs either. Just properly protype your functions and you should have no problems.

New example:

Code:
#include <stdio.h>

char * populateStr( void )
{
   char *pStr = NULL;

   pStr = (char *) malloc( sizeof(char) * 256 );
   strcpy( pStr, "Hello There" );

   return( pStr );
}

main()
{
   char * pStr2;

   pStr2 = populateStr();

   printf("String is [%s]\n", pStr2 );

   free( pStr2 );
}
 
Old 02-10-2002, 06:09 AM   #7
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Original Poster
Rep: Reputation: 30
tahnks alot :-)

it works !!!!!!

 
  


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
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
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM


All times are GMT -5. The time now is 02:29 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