www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - string init

reply kevinbealer yahoo.com writes:
Let's say I want to define a mutable string:

char[] a_tmp = "abcd";  // immutable ref
char[] a;
a[] = a_tmp; // mutable copy of immutable data.

OR..?

char[] a = "abc" ~ "d";

Is there a simpler or more direct syntax?

Kevin
May 03 2004
next sibling parent reply Andy Friesen <andy ikagames.com> writes:
kevinbealer yahoo.com wrote:
 Let's say I want to define a mutable string:
 
 char[] a_tmp = "abcd";  // immutable ref
 char[] a;
 a[] = a_tmp; // mutable copy of immutable data.
 
 OR..?
 
 char[] a = "abc" ~ "d";
 
 Is there a simpler or more direct syntax?
 
 Kevin
 
 

char[] a = ("abcd").dup; may work for you. -- andy
May 03 2004
parent Kevin Bealer <Kevin_member pathlink.com> writes:
In article <c76g4e$iau$1 digitaldaemon.com>, Andy Friesen says...
kevinbealer yahoo.com wrote:
 Let's say I want to define a mutable string:
 
 char[] a_tmp = "abcd";  // immutable ref
 char[] a;
 a[] = a_tmp; // mutable copy of immutable data.
 
 OR..?
 
 char[] a = "abc" ~ "d";
 
 Is there a simpler or more direct syntax?
 
 Kevin
 
 

char[] a = ("abcd").dup; may work for you. -- andy

Yep, that looks like the most logical. Kevin
May 04 2004
prev sibling next sibling parent Ben Hinkle <bhinkle4 juno.com> writes:
kevinbealer yahoo.com wrote:

 Let's say I want to define a mutable string:
 
 char[] a_tmp = "abcd";  // immutable ref
 char[] a;
 a[] = a_tmp; // mutable copy of immutable data.
 
 OR..?
 
 char[] a = "abc" ~ "d";
 
 Is there a simpler or more direct syntax?
 
 Kevin

The ".dup" property will duplicate an array: char[] a = a_tmp.dup;
May 03 2004
prev sibling parent Mike Wynn <one_mad_alien hotmail.com> writes:
On Mon, 3 May 2004 22:05:57 +0000 (UTC), kevinbealer yahoo.com wrote:

Let's say I want to define a mutable string:

char[] a_tmp = "abcd";  // immutable ref
char[] a;
a[] = a_tmp; // mutable copy of immutable data.

OR..?

char[] a = "abc" ~ "d";

Is there a simpler or more direct syntax?

Kevin

--------------------- import std.c.stdio; void nctest() { char[] a = "abcd"; char[]b = a; printf( "char[] a = 'acbd'\n" ); printf( "char[] b = a;\n=>\n" ); printf( "a=%.*s\n", a ); printf( "b=%.*s\n", b ); printf( "b[2]='2'...\n" ); b[2] = '2'; printf( "a=%.*s\n", a ); printf( "b=%.*s\n", b ); } void ctest() { const char[] a = "abcd"; char[]b = a; printf( "const char[] a = 'acbd'\n" ); printf( "char[] b = a;\n=>\n" ); printf( "b[2]='2'...\n" ); printf( "a=%.*s\n", a ); printf( "b=%.*s\n", b ); printf( "b[2]='2'...\n" ); b[2] = '2'; printf( "a=%.*s\n", a ); printf( "b=%.*s\n", b ); } int main( char[][] args ) { nctest(); ctest(); return 0; } ------------------------ output (as one would hope is) char[] a = 'acbd' char[] b = a; => a=abcd b=abcd b[2]='2'... a=ab2d b=ab2d const char[] a = 'acbd' char[] b = a; => b[2]='2'... a=abcd b=abcd b[2]='2'... a=abcd b=ab2d
May 06 2004