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

Date: Sat, 25 Dec 1999 16:31:26 +0100 (MET)
From: Tobias =?ISO-8859-1?Q?Ro=DFmann?= <t DOT rossmann AT gmx DOT at>
To: djgpp AT delorie DOT com
MIME-Version: 1.0
Subject: Re: Converting a string to a char *
X-Authenticated-Sender: #0002566928 AT gmx DOT net
X-Authenticated-IP: [212.185.249.183]
Message-ID: <7032.946135886@www7.gmx.net>
X-Mailer: WWW-Mail 1.5 (Global Message Exchange)
X-Flags: 0001
Reply-To: djgpp AT delorie DOT com

string::c_str returns a const char*. Thus, your first mistake was to
declare
b as char* without casting the returned value. Then you wrote
*b = *s.c_str(). This meant: assign the first character of string s to the
value pointed to by b. This is probably neither what you wanted to do, nor
is
it a good idea to use uninitialized pointers. The correct way would be to
assign the value returned from c_str to the pointer (not the pointed to
data).

int
main()
{
  string s = "test";
  const char *b;
  b = s.c_str();
  cout << b;
  return (0);
}

-- 
Sent through Global Message Exchange - http://www.gmx.net

- Raw text -


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