Re: a few doubts!
- From: Lew Pitcher <lpitcher@xxxxxxxxxxxx>
- Date: Sat, 30 Apr 2005 10:18:43 -0400
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
maadhuu wrote:
> #include<stdio.h>
> #include<conio.h>
conio.h is not a standard header. It's contents could be anything, and could
affect your program in ways we cannot determine
> #include<string.h>
> main()
main() takes either two arguments or none, and returns an int
you should use one of the two legal variants here, likely
int main(void)
> {
> char * a= "bcd";
a is declared as a pointer to an array of 4 char.
a can be altered
the array of 4 char cannot be altered
> clrscr();
clrscr() is not a standard function. It's actions and side-effects could do
anything, and will affect your program in ways we cannot determine.
> strcpy(a,"hello");
At least, undefined behaviour, because *a is only 4 char long, and the
side-effect (and function) of strcpy() would be to overwrite that space with a
6 char long value. 6 chars don't fit into a 4 char space.
The more knowledgable ones around here will tell you whether this is "illegal
behaviour" (because of the side-effect of strcpy(), which would be to attempt
to overwrite a char constant) or just "undefined behaviour".
> a = "fgh";
Legal. You are not replacing the constant char array, you are replacing the
variable pointer.
> a[0] = 't';
IIUC, this should be illegal.
> printf("%s",a);
main() returns an int. return one here.
(Unless, your compiler is C99 compliant, in which case, the return value is
optional and defaults to a pre-determined value).
> }
>
>
> now, in TC there is absolutely no error .....i thought it
> should........coz' when i declare a as a char * and assign it to some
> string then it should be a constant and cannot do things like a[0] = '4'
> and stuff........infact the entire thing here works properly....so why
> should it work properly??
The compiler is non-compliant?
The compiler is broken?
- --
Lew Pitcher
Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFCc5PDagVFX4UWr64RAs8TAJ4p+ZyxVWzyLHSghtGc9yAdO9altQCgkxAZ
TDeV/fVJ3HZ6ZPh/iGDjvb8=
=jTH7
-----END PGP SIGNATURE-----
.
- Follow-Ups:
- Re: a few doubts!
- From: Emmanuel Delahaye
- Re: a few doubts!
- From: maadhuu
- Re: a few doubts!
- From: Mark McIntyre
- Re: a few doubts!
- References:
- a few doubts!
- From: maadhuu
- a few doubts!
- Prev by Date: Re: main(int argc, char *argv[])
- Next by Date: Re: a few doubts!
- Previous by thread: a few doubts!
- Next by thread: Re: a few doubts!
- Index(es):
Relevant Pages
|