O
Old Wolf
Old said:#include <stdio.h>
#include <string.h>
int main(void)
{
if ( sizeof(char *) >= sizeof "abc" )
{
char *c;
c = (char *)&c;
strcpy(c,"abc");
puts(&c);
Of course I meant:
puts( (char *)&c );
Old said:#include <stdio.h>
#include <string.h>
int main(void)
{
if ( sizeof(char *) >= sizeof "abc" )
{
char *c;
c = (char *)&c;
strcpy(c,"abc");
puts(&c);
No, c is on the stack and so &c is pointing at one end of the free space
on the stack.
I think exactly one
byte can be safely written since we don't know in general which direction
the stack grows, or the endianess of the pointer.
Keith said:Or just
puts(c);
Joe Wright said:No. puts(c); will probably segfault (it does here) because c is
neither an array nor a pointer. This seems to "work"..
#include <stdio.h>
#include <string.h>
int main(void) {
char *c;
c = (char*)&c;
strcpy(c, "abc");
puts((char*)&c);
return 0;
}
c is neither an array nor a pointer.
Le 01-10-2005 said: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;
}
Here is a well defined program:
#include <stdio.h>
#include <string.h>
int main(){
void* p= &p;
if ( strlen("abc") < sizeof(p) ){
memcpy(p, "abc", strlen("abc")+1 );
puts( (char*) p );
Le 07-10-2005 said:It's not defined and these issues have
already been gone over in this thread.
p is an indeterminate pointer after the memcpy call.
Accessing the value of p at that point,
as the cast operator does, is undefined.
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
void *p = &p;
if (strlen("abc") < sizeof(p)) {
strcpy(p, "abc");
puts((char *)&p);
} else {
puts("Pointers too small");
}
return 0;
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.