Re: [Dev-C++] Help me.... strange char functions on MinGW
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Derek C. <de...@ci...> - 2008-08-02 22:05:52
|
When you type strset("TEST, TEST", 'c') you've pointed parameter s at the static string "TEST, TEST". That string can be put in a read only segment by the compiler, hence the access violation when you try and write to it. Not all implementations will do the same thing, hence it works in some circumstances. It is much better to keep writable data and read only initialisor data apart. so: main() { static char str[] = "TEST, TEST"; printf("%s", strset(str, 'c') ); getchar(); } This might look superficially the same, but this time str and the initialisor data "TEST, TEST" are two different things, and str will definitely be writable. Also for(i = 0; s[i]; i++) is syntactically correct, but it's really bad form. Better to write for(i = 0; s[i] != '\0' ; i++) making the test explicit. On Sat, Aug 2, 2008 at 7:17 PM, Rafael Oliveira Lima <raf...@gm...> wrote: > Hi All. > > I'm learning C with Dev-C++ and MinGW and a wrote such function like this: > > #include <stdio.h> > > char *strset(char *s, char ch); > > main() > { > printf("%s", strset("TEST, TEST", 'c') ); > getchar(); > } > > char *strset(char *s, char ch) > { > int i; > > for(i = 0; s[i]; i++) > s[i] = ch; > > return s; > } > > that would have to fill the string argument with the char ('c') and print > it. > > string: "TEST, TEST" > result: "**************" > > But, I have a Access Violation, and program crashes. > > I've tried to compile in LCCWin32 and runs fine. > Then, I've tried, again, to compile in mingw and ran on MSYS, suprise.... > works fine too. > > But in a DOS shell, the same mingw compiled program gives me a Access > Violation... Why!!!!???? > > Please Help... > -- > Rafael Oliveira de Lima > > Técnico Eletrônico - Projectus NDT. > |