Re: a few doubts!



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



Relevant Pages

  • Re: a few doubts!
    ... >to overwrite a char constant) or just "undefined behaviour". ... >The compiler is non-compliant? ... CLC FAQ ...
    (comp.lang.c)
  • Re: returning char arrays from a function
    ... But the compiler warning may be another ... the following program does not cause any warning ... char * funca{ ... above can lead to undefined behaviour as well. ...
    (comp.lang.c)
  • Re: A situation
    ... A char is simply an integral type of a certain width (defined ... > by the CHAR_BITS constant in the standard header limits.h, ... Randy Howard ...
    (comp.lang.c)
  • Re: Replacing fgets
    ... Even if u_int8_t is a typedef for unsigned char, ... Didn't your compiler complain here. ... offset is changed ... offset needs to be an int. ...
    (comp.lang.c)
  • Re: howto get rid of pointer arguments differ in signedness
    ... why don't you provide some argument supporting your 'total ... disagreement' with the idea that C-compilers should support ... Since the specification is ten years younger than 'the compiler', ... char and unsigned char are three mutually incomaptible types, ...
    (comp.os.linux.development.apps)