linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [jason@txt.com: Re: Problem with "chars"]
@ 2002-07-17 14:33 xlp
  2002-07-17 17:55 ` Elias Athanasopoulos
  0 siblings, 1 reply; 15+ messages in thread
From: xlp @ 2002-07-17 14:33 UTC (permalink / raw)
  To: linux-c-programming

This is Jason Winters reply, It was very usefull and My question was solved, I need to handle "strings" in my case.

thanks.

----- Forwarded message from "Jason P. Winters" <jason@txt.com> -----

From: "Jason P. Winters" <jason@txt.com>
Subject: Re: Problem with "chars"
To: xlp@emtel.net.co (xlp)
Date: Wed, 17 Jul 2002 06:45:46 -0700 (PDT)

 
> Hi C people, first of all, I'd like to thank you this mailing
> list and kernel.org project to give us a place where C programmers
> feel free to ask their questions and the support is excellent.
> I dont understand what's the way I should handle 'chars' on C. 
> I need to code a C function that returns a char, I want to do this:
> char foo();
> main(){
> 	char bar*;
> 	bar=foo();
> }

Hmmm.. Well, first, do you wish to return a single char, or a random
length of chars?  Your program kinda mixes the two, and you mention
below that you want to return a 'char', and then also mention you
want to return without a specific length.

These are two different return types.

char foo();

This returns a *single* char, always.  Just one. So your code could
have looked something like this:

main()
{
	char bar;
	bar = foo();
}

char foo
{
	char retbar;
	retbar = 'C';
	return(retbar);
}


Now, if you wish to return a random number of chars, it's called
an array.  And you'll need to return a pointer instead of a char.
So your main program could have looked like this:

char *foo();

main()
{
	char *bar;
	bar = foo();
}

Now we come to the tricky part... how to get a real string out of
foo().  You can't just do this:

char *foo()
{
	char retbar[20];
	strcpy(retbar, "Hi there!");
	return(retbar);
}

Because retbar isn't valid once you return. Local variables like that
are always created on the stack, once you return, the stack is
essentially invalidated (I mean here that it's available for reuse)
and it may or may not work, depending on when you use it again and
for what.  Bad move, that.  Causes nasty crashes when you least
expect it.

So, how do you do it?  You have to use malloc() and friends.
Your foo() routine could look something like this:

char *foo()
{
	char *retbar;
	retbar = (char *)malloc(20);
	strcpy(retbar, "Hi There!");
	return(retbar);
}

Malloc allocates a nice new set of memory on the heap, and you are
now returning the pointer to it.

NOTE!!!  In this sample code, there is NO error checking!  It's been
left out for clarity of the sample, and is NOT the way you should be
doing it!   The string "Hi There!" is obviously not something you would
bother returning, and it's only used as a point to say "copy some data
here".  A little better way to do that same routine (and the same
silly data) would be something like this:

char *foo()
{
	char *retbar;
	char *sample="Hi There!";

	if((retbar = (char *)malloc(strlen(sample)+1)) != (char *)0)
		strncpy(retbar, sample, strlen(sample));
	return(retbar);
}


So, there you have my humble offering.. hope that makes sense and
I didn't leave out *too* much (It's early here, haven't had my
coffee yet).

Ciao!

Jason
-- 
|UUCP  jason@txt.com                        Who wills, Can.
|VOICE (408) 243-3425                         Who tries, Does.
|LOCAL Hey, Jason!                              Who loves, Lives.          o_.
|Disclaimer:  Not me! I didn't do *THAT!*                                 <|
|Local Disclaimer:  I'm not here!                   A. McCaffrey           4


----- End forwarded message -----


^ permalink raw reply	[flat|nested] 15+ messages in thread
* RE: [jason@txt.com: Re: Problem with "chars"]
@ 2002-07-18  7:11 Alvarez Alberto-AALVARB1
  2002-07-18  9:25 ` Elias Athanasopoulos
  0 siblings, 1 reply; 15+ messages in thread
From: Alvarez Alberto-AALVARB1 @ 2002-07-18  7:11 UTC (permalink / raw)
  To: linux-c-programming

Hi all,
	i've always been a "malloc caster", so this had been good news for me.
	But i made a program and compiled it with gcc and g++, just for checking...

	My question raises after compiling a C program with g++ (g++ -v: gcc version 2.95.2 19991024 (release)) for Solaris, as i got this result:

	% g++ probo3.c
	probo3.c: In function `int main()':
	probo3.c:7: ANSI C++ forbids implicit conversion from `void *' in assignment

	while my program is this:

		#include <stdio.h>
		#include <stdlib.h>
	
		int main()
		{
		  char *aux;
		  aux=calloc(2, sizeof(char));
		}

	?Does anyone know the reason for this behaviour? I've always thought that C++ and C were almost the same at this kind of stuff.

Regards,

Alberto Alvarez Besada

Tlf.:        +34 914002155
e-mail.:   aalvarb1@motorola.com
            


> -----Original Message-----
> From: Glynn Clements [mailto:glynn.clements@virgin.net]
> Sent: miercoles, 17 de julio de 2002 20:54
> To: Jason P. Winters; eathan@otenet.gr; xlp@emtel.net.co;
> linux-c-programming@vger.kernel.org
> Subject: Re: [jason@txt.com: Re: Problem with "chars"]
> 
> 
> 
> Glynn Clements wrote:
> 
> > > If you don't cast it, you get a warning message:
> > > 
> > > tst.c: In function `foo':
> > > tst.c:17: warning: assignment makes pointer from integer 
> without a cast
> > 
> > Which should provide a clue that you forgot the prototype, which is
> > normally obtained using:
> > 
> > 	#include <stdlib.h>
> > 
> > An implicit cast between an *integer* (undeclared functions are
> > assumed to return "int") and any pointer *will* generate a warning
> > (unless the integer is a literal zero).
> > 
> > This is exactly *why* you *shouldn't cast* the result of malloc(),
> > because it will hide this problem.
> 
> In retrospect, it's worth mentioning that a more reliable approach is,
> where possible, to explictly enable warnings for undeclared 
> functions. 
> For gcc, this is controlled by the options:
> 
> `-Wimplicit-int'
>      Warn when a declaration does not specify a type.
> 
> `-Wimplicit-function-declaration'
> `-Werror-implicit-function-declaration'
>      Give a warning (or error) whenever a function is used 
> before being
>      declared.
> 
> `-Wimplicit'
>      Same as `-Wimplicit-int' and `-Wimplicit-function-'
>      `declaration'.
> 
> [Note that -Wimplicit is enabled by -Wall.]
> 
> While the "don't cast return types" rule is still useful (it shouldn't
> normally be necessary to cast return types, and may hide problems), it
> won't catch other cases (e.g. if the return value was assigned to an
> "int" variable, no warning would be generated, regardless of what the
> function actually returned).
> 
> -- 
> Glynn Clements <glynn.clements@virgin.net>
> -
> To unsubscribe from this list: send the line "unsubscribe 
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2002-07-24 22:38 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-07-17 14:33 [jason@txt.com: Re: Problem with "chars"] xlp
2002-07-17 17:55 ` Elias Athanasopoulos
2002-07-17 17:55   ` Jason P. Winters
2002-07-17 18:07     ` Glynn Clements
2002-07-17 18:24       ` Jason P. Winters
2002-07-17 18:54       ` Glynn Clements
2002-07-17 18:16     ` Elias Athanasopoulos
2002-07-17 19:30     ` Marius Nita
2002-07-18 14:43       ` Earl R. Lapus
2002-07-24 19:56       ` Mike Castle
2002-07-24 21:05         ` Jason P. Winters
2002-07-24 22:38           ` Glynn Clements
  -- strict thread matches above, loose matches on Subject: below --
2002-07-18  7:11 Alvarez Alberto-AALVARB1
2002-07-18  9:25 ` Elias Athanasopoulos
2002-07-18  9:18   ` Marius Nita

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).