G
grid
Hi,
A collegue of mine is of the opinion that the behaviour of the
following program is defined,but I am a little apprehensive.
#include<stdio.h>
#include<string.h>
int main()
{
char *c;
c = &c;
strcpy(c,"abc");
puts(&c);
retun 0;
}
The program prints the same value "abc" on multiple platforms and I even
tried it with multiple compilers.I can make out that its trying to write
to the pointer address and so probably the max it can write is 3 bytes +
'\0'.But even if I try to copy more that 4 bytes it prints the whole
string without any crashes.
Changed line >> strcpy(c,"abcdefg");
Now I know that this behaviour is undefined(writing more than 4 bytes)
as the sizeof the pointer is 4 bytes(on the machine I tested on).
Can anyone comment if this is compliant code and is the behaviour
guaranteed.
Thanks
~
A collegue of mine is of the opinion that the behaviour of the
following program is defined,but I am a little apprehensive.
#include<stdio.h>
#include<string.h>
int main()
{
char *c;
c = &c;
strcpy(c,"abc");
puts(&c);
retun 0;
}
The program prints the same value "abc" on multiple platforms and I even
tried it with multiple compilers.I can make out that its trying to write
to the pointer address and so probably the max it can write is 3 bytes +
'\0'.But even if I try to copy more that 4 bytes it prints the whole
string without any crashes.
Changed line >> strcpy(c,"abcdefg");
Now I know that this behaviour is undefined(writing more than 4 bytes)
as the sizeof the pointer is 4 bytes(on the machine I tested on).
Can anyone comment if this is compliant code and is the behaviour
guaranteed.
Thanks
~