Hi, I invite reviews for the following code: #include <stdio.h> #include <string.h> #include <stdlib.h> int main ( void ) { char *p; p = (char*) &p; strcpy ( p, "Hi" ); printf ( "%s\n", p ); return EXIT_SUCCESS; } Thanks. -- Vijay Kumar R Zanvar ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ On Wed, 24 Dec 2003 11:45:50 +0530, "Vijay Kumar R Zanvar" <vijoeyz@hotpop.com> wrote in comp.lang.c: > Hi, > > I invite reviews for the following code: Your code invokes undefined behavior. > > #include <stdio.h> > #include <string.h> > #include <stdlib.h> > > int > main ( void ) > { > char *p; p is an uninitialized pointer to char. > p = (char*) &p; p now contains the its own address. > strcpy ( p, "Hi" ); Now you overwrite p's contents with three characters, 'H', 'i', and '\0'. Immediate undefined behavior if sizeof (char *) is < 3, which is true on many 16-bit implementations. > printf ( "%s\n", p ); Undefined behavior for sure, you have modified the value of p via an lvalue of character type. Accessing it as a pointer, or indeed as anything other than an array of character type, is now undefined behavior. Undefined behavior also because printf() will attempt to dereference p, which almost certainly no longer points to a string your program has the right to access. > return EXIT_SUCCESS; > } > > > Thanks. What did you actually think this silly nonsense would be good for? -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "Vijay Kumar R Zanvar" <vijoeyz@hotpop.com> wrote in message news:bsbapi$bfn8a$1@ID-203837.news.uni-berlin.de... > Hi, > > I invite reviews for the following code: > > #include <stdio.h> > #include <string.h> > #include <stdlib.h> Includes ok. > > int > main ( void ) > { > char *p; > > p = (char*) &p; Why cast the pointers address to the pointer? WHen you operate on pointers, *p will give you accessto what is stored at the pointers address. Similar to ordinary variables: int p=5 printf ( "%d\n", p ); will yield 5 eq int *p = 5; printf ( "%d\n", *p ); will yield 5 also printf ( "%d\n", p ); will yield the address in memory where p is stored. Doing this cast will as always compile correctly, but yield a seg. fault. > strcpy ( p, "Hi" ); > printf ( "%s\n", p ); > return EXIT_SUCCESS; Assuming that EXIT_SUCCESS is 0 (simply put in a 'define EXIT_SUCCESS 0') > } > > > Thanks. > > -- > Vijay Kumar R Zanvar > My Home Page - http://www.oocities.org/vijoeyz/ > > -- I hope that this was nearby the answer you wished for. Ronny Mandal ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "Ronny Mandal" <ronnyma@math.uio.no> wrote: > When you operate on pointers, *p will give you access to what is > stored at the pointers address. Similar to ordinary variables: > > int p=5 ; > printf ( "%d\n", p ); will yield 5 It'll output the digit 5 and a newline character, yeah. > eq > > int *p = 5; This wrongly attempts to initialise a pointer type with an integer. It is a constraint violation, so the compiler must emit a diagnostic message. Perhaps you actually meant: int i = 5; int *p = &i; Now i has the value 5, and p has the value of the address of i. > printf ( "%d\n", *p ); will yield 5 also True, given my correction. > printf ( "%d\n", p ); will yield the address in memory where p is stored. This is undefined behaviour, as the %d conversion requires an int as its argument. The correct way to output a representation of the value of a pointer is: printf("%p\n", (void *)p); This converts the value of type 'pointer to int' into a value of type 'pointer to void' as required by the %p conversion specifier. > Assuming that EXIT_SUCCESS is 0 (simply put in a 'define EXIT_SUCCESS 0') No! EXIT_SUCCESS is a macro defined in <stdlib.h>, which the OP Vijay correctly included. It has the same meaning as returning 0, but need not actually have the value 0. You are not allowed to define this macro yourself, that would be undefined behaviour. -- Simon. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~