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-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
>
|