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