From: david DOT c DOT hoos DOT sr AT ada95 DOT com ("David C. Hoos, Sr.") Subject: Re: wxwin: Does anyone understand why this c-code fails 26 Oct 1996 21:50:02 -0700 Sender: daemon AT cygnus DOT com Approved: cygnus DOT gnu-win32 AT cygnus DOT com Distribution: cygnus Message-ID: <199610270422.XAA26280.cygnus.gnu-win32@vespucci.iquest.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Original-To: "Keith Gary Boyce" , X-MSMail-Priority: Normal X-Priority: 3 X-Mailer: Microsoft Internet Mail 4.70.1155 Original-Sender: owner-gnu-win32 AT cygnus DOT com Hi Keith, The code fails because you're attempting to alter memory which is read-only. This comes about from a faulty declaration for "name" in "main." Your declaration says "allocate space for a variable which is a pointer to char, and initialize it with the address of a string literal which you supply. The memory for the string literal is in a read-only data segment allocated by the compiler/linker. It is done this way, because if you had used an identical string literal in a similar context elsewhere in the same compilation unit, the compiler could have used the same memory for both. You can see, that in such a circumstance, allowing the program to alter such a string would be disastrous. In other words, the only VARIABLE you have declared is the variable holding the address of a string, and specified an initial value. What you need to do instead is to declare "name" as follows: char name [] = "C:\\tmp\\foo"; What this declaration says is allocate me space for a variable which is an array of char, sufficiently large to hold the string literal with which I'm initializing it, plus the terminating NUL. Now the characters are variable, which is what you want to do. Personally, it's stuff like this which makes me prefer to develop software in Ada, because an Ada compiler would never let you compile a program like this which compiles with no warnings or errors, then promptly crashes on execution. Hope this helps David C. Hoos, Sr. http://www.dbhwww.com http://www.ada95.com ---------- > From: Keith Gary Boyce > To: gnu-win32 AT cygnus DOT com > Subject: wxwin: Does anyone understand why this c-code fails > Date: Saturday, October 26, 1996 3:26 PM > > Does anyone know why the following code dumps when in > wxUnix2DosFilename function. I'm a little rusty on pointer stuff > #include > #include > #define WIN95 > #define wx_msw > > void > wxUnix2DosFilename (char *s) > { > // Yes, I really mean this to happen under DOS only! JACS > #ifdef wx_msw > int offset=0; > #ifdef WIN95 > if (isalpha (*s) && *(s + 1) == ':') > offset=2; > #endif > > if (s+offset) > while (*(s+offset)) > { > #ifdef WIN95 > if (*(s+offset) == '\\') > *s = '/'; > else > *s = *(s+offset); > #else > if (*s == '/') > *s = '\\'; > #endif > s++; > } > #ifdef WIN95 > *s=*(s+offset); > printf("%s\n",s); > #endif > #endif > } > > void main() > { > char c; > char *name = "C:\\tmp\\foo"; > FILE* f; > > wxUnix2DosFilename (name); > //f = fopen(name, "r"); > //while ((c = getc(f)) != EOF) { > //printf("%c", c); > //} /* while */ > //fclose(f); > } > > ------------------------------------- > Name: Garry Boyce > E-mail: garp AT opustel DOT opustel DOT com (Garry Boyce) > Date: 12/01/94 > Time: 08:46:10 > > This message was sent by Chameleon > ------------------------------------- > > - > For help on using this list, send a message to > "gnu-win32-request AT cygnus DOT com" with one line of text: "help". - For help on using this list, send a message to "gnu-win32-request AT cygnus DOT com" with one line of text: "help".