Thread View: alt.comp.lang.learn.c-c++
9 messages
9 total messages
Started by R
Tue, 27 May 2003 06:51
Return a string in C???
Author: R
Date: Tue, 27 May 2003 06:51
Date: Tue, 27 May 2003 06:51
13 lines
1561 bytes
1561 bytes
Hey all! Just learning C and I'm wondering how one would return a string from a function. Basically, I'm passing in a float and returning a 1-3 character string based on the value of the float. Is there a simple example that someone can provide? Thanks! P.s. Searching for "C" in Google and groups is a pain! : )
Re: Return a string in C???
Author: Jirka Klaue
Date: Tue, 27 May 2003 09:28
Date: Tue, 27 May 2003 09:28
14 lines
1600 bytes
1600 bytes
R wrote: > Hey all! > > Just learning C and I'm wondering how one would return a string from a > function. > > Basically, I'm passing in a float and returning a 1-3 character string based > on the value of the float. > > Is there a simple example that someone can provide? return "42"; Jirka
Re: Return a string in C???
Author: Adrian Parker
Date: Mon, 02 Jun 2003 14:46
Date: Mon, 02 Jun 2003 14:46
29 lines
1932 bytes
1932 bytes
"R" <R@R.R> wrote in message news:oVDAa.85219$Vi5.2444320@news1.calgary.shaw.ca... > Hey all! > > Just learning C and I'm wondering how one would return a string from a > function. > > Basically, I'm passing in a float and returning a 1-3 character string based > on the value of the float. > > Is there a simple example that someone can provide? > > Thanks! > > P.s. Searching for "C" in Google and groups is a pain! : ) Why would you search for "C", which is very generalised and likely to return several million records, as opposed to any of these more specific searches: +c +"return string from function" +"c programming" +"return string" etc Adrian
Re: Return a string in C???
Author: chris
Date: Mon, 02 Jun 2003 17:42
Date: Mon, 02 Jun 2003 17:42
27 lines
1746 bytes
1746 bytes
"R" <R@R.R> wrote in message news:<oVDAa.85219$Vi5.2444320@news1.calgary.shaw.ca>...
> Hey all!
>
> Just learning C and I'm wondering how one would return a string from a
> function.
>
> Basically, I'm passing in a float and returning a 1-3 character string based
> on the value of the float.
>
> Is there a simple example that someone can provide?
>
> Thanks!
>
> P.s. Searching for "C" in Google and groups is a pain! : )
you could have the function allocate the buffer and return the length:
int StringFunction (char ** buffer) {
int bufferSize;
...
*buffer = (char *)malloc(bufferSize);
...
return bufferSize;
}
chris
Re: Return a string in C???
Author: Richard Heathfield
Date: Mon, 02 Jun 2003 17:56
Date: Mon, 02 Jun 2003 17:56
23 lines
1867 bytes
1867 bytes
chris wrote:
<snip>
>
>
> you could have the function allocate the buffer and return the length:
>
> int StringFunction (char ** buffer) {
> int bufferSize;
> ...
> *buffer = (char *)malloc(bufferSize);
What does the cast buy you?
And why use int, when malloc takes size_t?
<snip>
--
Richard Heathfield : binary@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Re: Return a string in C???
Author: chris
Date: Tue, 03 Jun 2003 12:45
Date: Tue, 03 Jun 2003 12:45
31 lines
2291 bytes
2291 bytes
"Richard Heathfield" <dontmail@address.co.uk.invalid> wrote in message
news:bbg354$k2n$3@hercules.btinternet.com...
> chris wrote:
>
> <snip>
> >
> >
> > you could have the function allocate the buffer and return the length:
> >
> > int StringFunction (char ** buffer) {
> > int bufferSize;
> > ...
> > *buffer = (char *)malloc(bufferSize);
>
> What does the cast buy you?
>
> And why use int, when malloc takes size_t?
>
> <snip>
>
> --
> Richard Heathfield : binary@eton.powernet.co.uk
> "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
> C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
> K&R answers, C books, etc: http://users.powernet.co.uk/eton
my compiler complains about casting a void * to a value without doing it
explicitly, and i would normally use an unsigned, but didn't think of it at
the time because it was just a quick response
chris
Re: Return a string in C???
Author: Karl Heinz Buchegger
Date: Tue, 03 Jun 2003 14:39
Date: Tue, 03 Jun 2003 14:39
40 lines
2341 bytes
2341 bytes
chris wrote:
>
> "Richard Heathfield" <dontmail@address.co.uk.invalid> wrote in message
> news:bbg354$k2n$3@hercules.btinternet.com...
> > chris wrote:
> >
> > <snip>
> > >
> > >
> > > you could have the function allocate the buffer and return the length:
> > >
> > > int StringFunction (char ** buffer) {
> > > int bufferSize;
> > > ...
> > > *buffer = (char *)malloc(bufferSize);
> >
> > What does the cast buy you?
> >
> > And why use int, when malloc takes size_t?
> >
> > <snip>
> >
> > --
> > Richard Heathfield : binary@eton.powernet.co.uk
> > "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
> > C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
> > K&R answers, C books, etc: http://users.powernet.co.uk/eton
>
> my compiler complains about casting a void * to a value without doing it
> explicitly,
Then you are not using a C compiler, but are using a C++ compiler.
In this case you should drop malloc altogether and use new instead.
--
Karl Heinz Buchegger
kbuchegg@gascad.at
Re: Return a string in C???
Author: Mark McIntyre
Date: Tue, 03 Jun 2003 14:46
Date: Tue, 03 Jun 2003 14:46
27 lines
2961 bytes
2961 bytes
On Tue, 03 Jun 2003 12:45:51 GMT, in comp.lang.c , "chris" <ChrisCummings@hot.rr.com> wrote: >"Richard Heathfield" <dontmail@address.co.uk.invalid> wrote in message >news:bbg354$k2n$3@hercules.btinternet.com... >> What does the cast buy you? >> >my compiler complains about casting a void * to a value without doing it >explicitly, and i would normally use an unsigned, but didn't think of it at >the time because it was just a quick response Thats because you're compiling C++ not C. If you've a C++ question then CLC is the wrong place to ask, and you may get wrong answers due to the subtle differences between C and C++. Also your newsreader is broken, its not properly trimming people's sigs off. Can you check that out? -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html> ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Re: Return a string in C???
Author: chris
Date: Thu, 05 Jun 2003 12:26
Date: Thu, 05 Jun 2003 12:26
7 lines
1871 bytes
1871 bytes
sorry, i don't post to newsgroups all that often so i'm not familiar with all the courtesies i'm supposed to go through when posting :) i also wasn't aware of the explicit cast being required only in c++, so thanks for that knowledge anyway. i'd also just like to point out that the question wasn't mine, i was trying to help a guy out. anyway...later chris
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads