Thread: [Dev-C++] Help me.... strange char functions on MinGW
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Rafael O. L. <raf...@gm...> - 2008-08-02 18:17:23
|
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. Programador Junior e Depurador Assembly em Arquiteturas RISC Microchip 8 bits. (famílias PIC12*, PIC14*, PIC16* e PIC18*). |
From: Eric <eri...@wo...> - 2008-08-02 21:12:17
|
yes well I got a "this program has performed an illegal operation and will shut down" untill I changed main() to int main() in theory it should go int main(int argc, char *argv[]) with a return 0; or maybe something else at the end of the main function the group looks forward to question two on this project try this #include <stdio.h> #include <cstdlib> #include <iostream> char *strset(char *s, char ch); int main() { printf("%s", "TEST, TEST", 'c' ); getchar(); system("PAUSE"); return EXIT_SUCCESS; } char *strset(char *s, char ch) { int i; for(i = 0; s[i]; i++) s[i] = ch; return s; } If you are going to do HTML EMail, do it with class >From Eric ----- Original Message ----- From: Rafael Oliveira Lima To: dev...@li... Sent: Sunday, August 03, 2008 6:17 AM Subject: [Dev-C++] Help me.... strange char functions on MinGW 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. Programador Junior e Depurador Assembly em Arquiteturas RISC Microchip 8 bits. (famílias PIC12*, PIC14*, PIC16* e PIC18*). ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users No virus found in this incoming message. Checked by AVG. Version: 7.5.524 / Virus Database: 270.5.10/1584 - Release Date: 31-Jul-08 12:00 |
From: Eric <eri...@wo...> - 2008-08-02 21:42:17
|
rats!! I forgot to tell you I changed printf("%s", strset("TEST, TEST", 'c') ); to printf("%s", "TEST, TEST", 'c' ); that is why I thought "there is bound to be a question two" ======> Note the file size of this EMail!!! <======= compared to the last two >From Eric ----- Original Message ----- From: Eric To: Rafael Oliveira Lima ; dev...@li... Sent: Sunday, August 03, 2008 9:09 AM Subject: Re: [Dev-C++] Help me.... strange char functions on MinGW yes well I got a "this program has performed an illegal operation and will shut down" untill I changed main() to int main() in theory it should go int main(int argc, char *argv[]) with a return 0; or maybe something else at the end of the main function the group looks forward to question two on this project try this #include <stdio.h> #include <cstdlib> #include <iostream> char *strset(char *s, char ch); int main() { printf("%s", "TEST, TEST", 'c' ); getchar(); system("PAUSE"); return EXIT_SUCCESS; } char *strset(char *s, char ch) { int i; for(i = 0; s[i]; i++) s[i] = ch; return s; } If you are going to do HTML EMail, do it with class >From Eric ----- Original Message ----- From: Rafael Oliveira Lima To: dev...@li... Sent: Sunday, August 03, 2008 6:17 AM Subject: [Dev-C++] Help me.... strange char functions on MinGW 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. Programador Junior e Depurador Assembly em Arquiteturas RISC Microchip 8 bits. (famílias PIC12*, PIC14*, PIC16* e PIC18*). |
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. > |
From: Eric <eri...@wo...> - 2008-08-03 02:21:00
|
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. > ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users -- No virus found in this incoming message. Checked by AVG. Version: 7.5.524 / Virus Database: 270.5.10/1587 - Release Date: 02-Aug-08 17:30 |
From: Per W. <pw...@ia...> - 2008-08-03 17:47:34
|
"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. > > > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's challenge > Build the coolest Linux based applications with Moblin SDK & win great > prizes > Grand prize is a trip for two to an Open Source event anywhere in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > -- > No virus found in this incoming message. > Checked by AVG. > Version: 7.5.524 / Virus Database: 270.5.10/1587 - Release Date: 02-Aug-08 > 17:30 > > > > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's challenge > Build the coolest Linux based applications with Moblin SDK & win great prizes > Grand prize is a trip for two to an Open Source event anywhere in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
From: Derek C. <de...@ci...> - 2008-08-03 20:38:37
|
There's a key dfference between C and C++ here. in C int main() means main will accept any number of parameters in any order, whereas in C++ int main() is the same as the c int main(void), i.e. main takes no parameters. Strictly they're both wrong though. In most operating systems the signature of main is in fact int main(int argc, char* argv[]); This is so command line arguments can be passed. On Sun, Aug 3, 2008 at 6:47 PM, Per Westermark <pw...@ia...> wrote: > "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. >> > >> >> ------------------------------------------------------------------------- >> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge >> Build the coolest Linux based applications with Moblin SDK & win great >> prizes >> Grand prize is a trip for two to an Open Source event anywhere in the world >> http://moblin-contest.org/redirect.php?banner_id=100&url=/ >> _______________________________________________ >> Dev-cpp-users mailing list >> Dev...@li... >> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >> >> >> -- >> No virus found in this incoming message. >> Checked by AVG. >> Version: 7.5.524 / Virus Database: 270.5.10/1587 - Release Date: 02-Aug-08 >> 17:30 >> >> >> >> >> ------------------------------------------------------------------------- >> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge >> Build the coolest Linux based applications with Moblin SDK & win great prizes >> Grand prize is a trip for two to an Open Source event anywhere in the world >> http://moblin-contest.org/redirect.php?banner_id=100&url=/ >> _______________________________________________ >> Dev-cpp-users mailing list >> Dev...@li... >> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >> > > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's challenge > Build the coolest Linux based applications with Moblin SDK & win great prizes > Grand prize is a trip for two to an Open Source event anywhere in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
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. > |
From: Per W. <pw...@ia...> - 2008-08-03 22:17:52
|
In the old TurboC days, there where a lot of programs with void main() but even then, the return value should have been int, to allow batch files to test the ERRORLEVEL variable. A program not returning with a explicit "return status" - or a call to exit(status) - will have a return value of 0. /pwm On Mon, 4 Aug 2008, Eric wrote: > 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. > > > > > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's challenge > Build the coolest Linux based applications with Moblin SDK & win great prizes > Grand prize is a trip for two to an Open Source event anywhere in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |