www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - copy char to char[]

reply nix <nix_member pathlink.com> writes:
Hello, 

this Programm run fine under Windows: 

import std.stdio; 
int main() { 

char[] str = "123456"; 
str[0] ='A'; 
writefln("str = %s",str); 
return 0; 
} 

but didn't run under linux. (Speicherzugriffsfehler) 
Can this be a problem from my character set? 

LANG=de_DE.UTF-8 
LC_CTYPE="de_DE.UTF-8" 
LC_NUMERIC="de_DE.UTF-8" 
LC_TIME="de_DE.UTF-8" 
LC_COLLATE="de_DE.UTF-8" 
LC_MONETARY="de_DE.UTF-8" 
LC_MESSAGES="de_DE.UTF-8" 
LC_PAPER="de_DE.UTF-8" 
LC_NAME="de_DE.UTF-8" 
LC_ADDRESS="de_DE.UTF-8" 
LC_TELEPHONE="de_DE.UTF-8" 
LC_MEASUREMENT="de_DE.UTF-8" 
LC_IDENTIFICATION="de_DE.UTF-8" 
LC_ALL=de_DE.UTF-8 

My system in Debian testing. 
dmd 0.118 
Mar 17 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
nix wrote:

 this Programm run fine under Windows: 
 
 import std.stdio; 
 int main() { 
 
 char[] str = "123456"; 
 str[0] ='A'; 
 writefln("str = %s",str); 
 return 0; 
 } 
 
 but didn't run under linux. (Speicherzugriffsfehler) 
 Can this be a problem from my character set? 
No, it's because string literals are read/write on Windows and read-only on Linux and other platforms... It's a known "platform-specific behaviour" of D. (It is solved in C/C++ by using "const char *") Short consequence, use: char[] str = "123456".dup; That string will be read-write on every D platform... --anders PS. German "Speicherzugriffsfehler" is known as segmentation fault (or segfault) in English...
Mar 17 2005
parent reply nix <nix_member pathlink.com> writes:
Thanks a lot. 
What a trap. :-) 

In article <d1c62o$1438$1 digitaldaemon.com>, 
=?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says... 
 
nix wrote: 
 
 this Programm run fine under Windows:  
  
 import std.stdio;  
 int main() {  
  
 char[] str = "123456";  
 str[0] ='A';  
 writefln("str = %s",str);  
 return 0;  
 }  
  
 but didn't run under linux. (Speicherzugriffsfehler)  
 Can this be a problem from my character set?  
No, it's because string literals are read/write on Windows and read-only on Linux and other platforms... It's a known "platform-specific behaviour" of D. (It is solved in C/C++ by using "const char *") Short consequence, use: char[] str = "123456".dup; That string will be read-write on every D platform... --anders PS. German "Speicherzugriffsfehler" is known as segmentation fault (or segfault) in English...
Mar 17 2005
parent reply "uframer" <uframer sina100.com.cn> writes:
What a weired behaviour!
"nix" <nix_member pathlink.com> 写入消息新闻:d1e15c$858$1 digitaldaemon.com...
 Thanks a lot.
 What a trap. :-)

 In article <d1c62o$1438$1 digitaldaemon.com>,
 =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says...
nix wrote:

 this Programm run fine under Windows:

 import std.stdio;
 int main() {

 char[] str = "123456";
 str[0] ='A';
 writefln("str = %s",str);
 return 0;
 }

 but didn't run under linux. (Speicherzugriffsfehler)
 Can this be a problem from my character set?
No, it's because string literals are read/write on Windows and read-only on Linux and other platforms... It's a known "platform-specific behaviour" of D. (It is solved in C/C++ by using "const char *") Short consequence, use: char[] str = "123456".dup; That string will be read-write on every D platform... --anders PS. German "Speicherzugriffsfehler" is known as segmentation fault (or segfault) in English...
Mar 18 2005
parent reply "Regan Heath" <regan netwin.co.nz> writes:
Unfortunately not so weird if you use the M$ C/C++ compiler for Win32 and  
cc/gcc for Unix.
The exact same thing occurs.

On Fri, 18 Mar 2005 21:45:20 +0800, uframer <uframer sina100.com.cn> wrote:
 What a weired behaviour!
 "nix" <nix_member pathlink.com> 脨 
 麓脠毛脧没脧垄脨脗脦脜:d1e15c$858$1 digitaldaemon.com...
 Thanks a lot.
 What a trap. :-)

 In article <d1c62o$1438$1 digitaldaemon.com>,
 =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says...
 nix wrote:

 this Programm run fine under Windows:

 import std.stdio;
 int main() {

 char[] str = "123456";
 str[0] ='A';
 writefln("str = %s",str);
 return 0;
 }

 but didn't run under linux. (Speicherzugriffsfehler)
 Can this be a problem from my character set?
No, it's because string literals are read/write on Windows and read-only on Linux and other platforms... It's a known "platform-specific behaviour" of D. (It is solved in C/C++ by using "const char *") Short consequence, use: char[] str = "123456".dup; That string will be read-write on every D platform... --anders PS. German "Speicherzugriffsfehler" is known as segmentation fault (or segfault) in English...
Mar 18 2005
parent reply Jan-Eric Duden <jeduden whisset.com> writes:
Regan Heath wrote:
 Unfortunately not so weird if you use the M$ C/C++ compiler for Win32 
 and  cc/gcc for Unix.
 The exact same thing occurs.
Except in C++ you would declare it as a const char array....
Jun 22 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
On Wed, 22 Jun 2005 13:07:14 +0200, Jan-Eric Duden <jeduden whisset.com>  
wrote:
 Regan Heath wrote:
 Unfortunately not so weird if you use the M$ C/C++ compiler for Win32  
 and  cc/gcc for Unix.
 The exact same thing occurs.
Except in C++ you would declare it as a const char array....
Unless you forgot to add that little word "const". Really the compiler knows it's const without our telling it, it put it in read only memory after all (if only on linux). Regan
Jun 22 2005