Re: [Dev-C++] Dev-cpp-users Digest, Vol 27, Issue 3
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: hilmy <hi...@gm...> - 2008-08-05 04:03:33
|
try this #include <stdio.h> using namespace std; char *strset(const char *s, char ch); main() { printf("%s", strset("TEST, TEST", 'c') ); getchar(); } char *strset(const char *s, char ch) { int i; char *x; x=new char[sizeof(s)+1]; for(i = 0; s[i]!='\0'; i++) x[i] = ch; x[i]='\0'; return x; } thank u, hilmy On Mon, Aug 4, 2008 at 9:19 AM, <dev...@li... > wrote: > Send Dev-cpp-users mailing list submissions to > dev...@li... > > To subscribe or unsubscribe via the World Wide Web, visit > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > or, via email, send a message with subject or body 'help' to > dev...@li... > > You can reach the person managing the list at > dev...@li... > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Dev-cpp-users digest..." > > > Today's Topics: > > 1. Re: Help me.... strange char functions on MinGW (Per Westermark) > 2. Re: Help me.... strange char functions on MinGW (Derek Clarke) > 3. Re: Help me.... strange char functions on MinGW (Eric) > 4. Re: Help me.... strange char functions on MinGW (Per Westermark) > 5. Re: Help me.... strange char functions on MinGW > (Rafael Oliveira Lima) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Sun, 3 Aug 2008 19:47:18 +0200 (CEST) > From: Per Westermark <pw...@ia...> > Subject: Re: [Dev-C++] Help me.... strange char functions on MinGW > To: Eric <er...@du...> > Cc: dev...@li... > Message-ID: > <Pin...@ia...> > Content-Type: TEXT/PLAIN; charset=ISO-8859-1 > > "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 > > > > > > > ------------------------------ > > Message: 2 > Date: Sun, 3 Aug 2008 21:38:34 +0100 > From: "Derek Clarke" <de...@ci...> > Subject: Re: [Dev-C++] Help me.... strange char functions on MinGW > To: dev...@li... > Message-ID: > <efe...@ma...> > Content-Type: text/plain; charset=ISO-8859-1 > > 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 > > > > > > ------------------------------ > > Message: 3 > Date: Mon, 4 Aug 2008 09:38:05 +1200 > From: "Eric" <eri...@wo...> > Subject: Re: [Dev-C++] Help me.... strange char functions on MinGW > To: <dev...@li...> > Message-ID: <001b01c8f5b1$37aa8e00$8e60d3cb@one> > Content-Type: text/plain; charset="iso-8859-1" > > 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. > > > > > > > > ------------------------------ > > Message: 4 > Date: Mon, 4 Aug 2008 00:17:38 +0200 (CEST) > From: Per Westermark <pw...@ia...> > Subject: Re: [Dev-C++] Help me.... strange char functions on MinGW > To: Eric <er...@du...> > Cc: dev...@li... > Message-ID: > <Pin...@ia...> > Content-Type: TEXT/PLAIN; charset=ISO-8859-1 > > 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 > > > > > > > ------------------------------ > > Message: 5 > Date: Mon, 4 Aug 2008 00:49:17 -0300 > From: "Rafael Oliveira Lima" <raf...@gm...> > Subject: Re: [Dev-C++] Help me.... strange char functions on MinGW > To: dev...@li... > Message-ID: > <959...@ma...> > Content-Type: text/plain; charset="iso-8859-1" > > Ok Great People! I really thanks you Eric and Derek to answer my question! > > For now I?m using variables it?s better. > > I thought that's to be a reason for some compilers ( LCCWin32) do what i > said before, but each have particularity. As I prefer the Dev-C++ and MinGW > (3.4.5), it?s better to respect the way of code if I want to port for gcc > on > Linux. > > I pick the advices of making explicit tests ( I tried to make use of pure > logic, but You're right Derek). > > Once again... thanks and regards! > > -- > 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*). > -------------- next part -------------- > An HTML attachment was scrubbed... > > ------------------------------ > > ------------------------------------------------------------------------- > 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://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > End of Dev-cpp-users Digest, Vol 27, Issue 3 > ******************************************** > |