Re: [Dev-C++] Help me.... strange char functions on MinGW
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Eric <eri...@wo...> - 2008-08-03 21:40:52
|
No I think at the time it was right but maybe the rules have been changed and now it is wrong. ----- Original Message ----- From: Per Westermark <pw...@ia...> To: Eric <er...@du...> Cc: <dev...@li...> Sent: Monday, August 04, 2008 5:47 AM Subject: Re: [Dev-C++] Help me.... strange char functions on MinGW "I was taught on day one of C/C++ to use 'void main()'" You where taught wrong. main() should always return int, _unless_ you are working with embedded programming, where there often is no OS to return back to. The return value from main() is the return value that the OS will receive back when the application ends, i.e. can be used by a batch file to decide how to continue. In this case, the compiler will default any missing type declarations to int, so: main() { } is a short form of: int main(void) { } However, it is strongly recommended to always speicfy the int type explicitly (except in relation to short, long or unsigned, in which case it is quite common to skip the 'int' part). /pwm On Sun, 3 Aug 2008, Eric wrote: > would it not be be a good idea get into a the practice of useing a bit more > than "main()" > to start the main function > I was taught on day one of C/C++ to use "void main()" > > > I know I am pulling out hairs here one by one when I should be giving a > complete hair cut. > > > maybe I should of re-leant a bit more on "strnset - strset - Set Bytes in > String" before replying to question > > > >From Eric > > ----- Original Message ----- > From: Derek Clarke <de...@ci...> > To: Rafael Oliveira Lima <raf...@gm...> > Cc: <dev...@li...> > Sent: Sunday, August 03, 2008 10:05 AM > Subject: Re: [Dev-C++] Help me.... strange char functions on MinGW > > > 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. > |