www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/12/25/19:16:32

From: "Andrew R. Gillett" <arganoid AT fatal-design DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Converting a string to a char *
Date: Sat, 25 Dec 1999 14:55:59 -0000
Organization: www.release-dates.co.uk
Lines: 30
Message-ID: <MPG.12ceeb5dd57db0a298b796@news.freeserve.net>
References: <840i65$auc$1 AT bob DOT news DOT rcn DOT net> <MPG DOT 12ce1d301f91f34e98b78e AT news DOT freeserve DOT net> <auV84.1816$GS5 DOT 3306 AT newsfeed DOT slurp DOT net>
NNTP-Posting-Host: modem-179.lemonpeel-angel.dialup.pol.co.uk
X-Trace: newsg2.svr.pol.co.uk 946133778 13867 62.137.38.179 (25 Dec 1999 14:56:18 GMT)
NNTP-Posting-Date: 25 Dec 1999 14:56:18 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Newsreader: MicroPlanet Gravity v2.20
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

In comp.os.msdos.djgpp, bowman wrote:
> Andrew R. Gillett <arganoid AT fatal-design DOT com> wrote:
> 
> >> *b = *s.c_str();
> >
> >Change this to:
> >
> >strcpy (b, s.c_str());
> 
> b is a pointer to char. s.c_str() returns a pointer to char. So,
> b = s.c_str() would work.

I don't know exactly how c_str() works - does the char array created by 
it continue to exist after the line on which it is called?


> Doing a strcpy of some arbitrarily long string to a pointer that was
> never initialized will dump core and die, if you are lucky.  

Good point. Tom, if you want to use strcpy, you should define b as a char 
array with enough elements so that any string you put into it will fit. 
You could also do it by checking the length of s, and then reserving some 
memory for b to point to based on the amount of space the string will 
take up.


-- 
Andrew Gillett      http://argnet.fatal-design.com/     ICQ: See homepage

Abducensnerve

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019