Thread: Pointer pointing to itself

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    7

    Pointer pointing to itself

    Code:
    include<conio.h>
    #include<stdio.h>
    void main()
    {
    int number = 100;
    int *numPtr =(&numPtr);
    clrscr();  //sorry for the wrong positioning earlier :(
    if(numPtr == *numPtr)
    {
    printf("wow!!!");
    }
    printf("hello!!! mr. %d %d %d",++*numPtr,numPtr,numPtr+1);
    getch();
    }
    OUTPUT :
    hello!!! mr. -13 -14 -12


    Query 1 : Why is "wow" not printed?...The pointer 'numPtr' contains its own address(since it points to itself), and since the dereference happens on this 'self pointer' (*numPtr), it refers to the contents of 'numPtr' . Then why does it not print "wow"?
    When i replace the above 'if' statement with 'if(numPtr != *numPtr)', amazingly the "wow" is again not printed?

    Query 2:
    The output too is confusing.
    My numPtr somehow gets the value -14(ok...fine).
    ' ++*numPtr ' does -14+1 = -13(fine..again)
    Now printing ' numPtr ' should give me -13(the address it contains, but the output shows -14)
    Lets assume ' numPtr ' contains -14. Now ' numPtr+1 ' should give -13, but the output gives me -12.

    Please help! . Am i making some basic conceptual mistake?

    Thanks in advance
    Last edited by rrahulvverma; 07-20-2010 at 05:59 AM. Reason: Placed my 'clrscr()' at the wrong place...(but even now i dont get the "wow")

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What is the type of numPtr?
    What is the type of &numPtr?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) Is fundamentally broken. A pointer to a pointer is not a pointer. So if you have an int, and take the address of that, you get an int*. Now, take the address of that and you get an int**. Type mismatch and therefore you are getting undefined behavior. Don't bother trying to figure it out. It just. won't. work. The end.

    2) Where is the code?

    3) Use int main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    Code:
    int number = 100;
    int *numPtr =(&numPtr);
    if(numPtr == *numPtr)
    {
    printf("wow!!!");
    }
    clrscr();
    Does clrscr(); have anything to do with not seeing "wow!!!", I don't that function available to me.

    >Now printing ' numPtr ' should give me -13(the address it contains, but the output shows -14)
    No, the address of numPtr is still -14 ( or the hexadecimal equivalent ). You only changed the value pointed to by numPtr in the last operation. This goes for the last question as well.

    [EDIT : REASON: In reply to other posts]
    Am I wrong in saying that &numPtr will be assumed by the compiler to be the address of an integer variable through implicit type coercion and hence when dereferenced it will retrieve the value of the address of numPtr in integer form?
    Last edited by CSaw; 07-19-2010 at 07:24 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    clrscr is a non-standard function that clears the console screen.
    The reason for not seeing the wow is undefined behavior. That statement is simply broken is so many horrible ways. Just give it some thought.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    Quote Originally Posted by Elysia View Post
    2) Where is the code?

    Code:
    //....
    printf("hello!!! mr. %d %d %d",++*numPtr,numPtr,numPtr+1);
    //....
    From original post.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by CSaw View Post
    Code:
    int number = 100;
    int *numPtr =(&numPtr);
    if(numPtr == *numPtr)
    {
    printf("wow!!!");
    }
    clrscr();
    Does clrscr(); have anything to do with not seeing "wow!!!", I don't that function available to me.

    >Now printing ' numPtr ' should give me -13(the address it contains, but the output shows -14)
    No, the address of numPtr is still -14 ( or the hexadecimal equivalent ). You only changed the value pointed to by numPtr in the last operation. This goes for the last question as well.

    [EDIT : REASON: In reply to other posts]
    Am I wrong in saying that &numPtr will be assumed by the compiler to be the address of an integer variable through implicit type coercion and hence it will receive the value of the address of numPtr in integer form?
    Firstly, you are wrong in not following the forum rules and posting ON THE SAME THREAD from multiple accounts just to confuse the hell out of us.

    Secondly, the compiler doesn't assume anything, because unlike us it can't ponder things. All the compiler sees is <token> = <token>.

    Thirdly, numPtr will not equal *numPtr(unless by some crazy chance) because numPtr is an address of an int and *numPtr is the int contained at that address. Also, numPtr is not even pointing to some VALID memory address therefore *numPtr could be anything. That is why WOW will probably NEVER get printed. However, as the posts above have indicated, welcome to the realm of undefined behavior.

    EDIT: Disregard the first paragraph. I thought you were the same person.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CSaw View Post
    Code:
    //....
    printf("hello!!! mr. %d %d %d",++*numPtr,numPtr,numPtr+1);
    //....
    From original post.
    Ah, that. Well, once again undefined behavior. You cannot write and read from the same variable in the same statement because the order of evaluation is implementation defined.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    Actually I tried it out on my compiler and wow was printed...here is my code:

    Code:
    int main()
    {
    	int *numPtr = &numPtr;
    	if(numPtr == *numPtr)
    	{
    		puts("woow");
    	}
    	return 0;
    }
    /* OUTPUT VIA DEBUGGER */
    // numPtr = 0x0012ff54
    // *numPtr = 1245012
    // 0x0012ff54 is the same as 1245012 in decimal
    However I hear what you both ( claudiu and Elysia ) are saying about undefined behaviour but this makes sense to me, because it dereferences the value of numPtr and converts to an int, both of them are the same because it is pointing to itself. Am I getting this all wrong?

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    *(&pointer) = pointer;

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    Exactly my point Bayint! Let's look at it this way:

    numPtr = &numPtr;

    numPtr == *numPtr

    but numPtr = &numPtr:

    (&numPtr) == *(&numPtr) => &numPtr == numPtr

    but we said numPtr = &numPtr :

    &numPtr == &numPtr

    wow! indeed.

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by CSaw View Post
    Actually I tried it out on my compiler and wow was printed...here is my code:

    Code:
    int main()
    {
    	int *numPtr = &numPtr;
    	if(numPtr == *numPtr)
    	{
    		puts("woow");
    	}
    	return 0;
    }
    /* OUTPUT VIA DEBUGGER */
    // numPtr = 0x0012ff54
    // *numPtr = 1245012
    // 0x0012ff54 is the same as 1245012 in decimal
    However I hear what you both ( claudiu and Elysia ) are saying about undefined behaviour but this makes sense to me, because it dereferences the value of numPtr and converts to an int, both of them are the same because it is pointing to itself. Am I getting this all wrong?
    Whatever compiler you are using to compile that is retarded. Any decent compiler will warn you not to assign incompatible pointer types to one another.

    Here is what GCC has to say about that piece of code:

    "test.c: In function `main':
    test.c:5: warning: initialization from incompatible pointer type
    test.c:6: warning: comparison between pointer and integer
    "

    As a rule of thumb, in 99% of cases, don't try to outsmart the compiler. You won't.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  13. #13
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    Oh no, I got a warning as well, but it will still compile and run. Yes, it is probably not a good idea assigning to a variable using different levels of indirection, but that wasn't what I was debating, I was debating that it still printed wow.

    And my compiler is Visual Studio, just in case you were wondering.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The fact that you get those warnings means it's undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Btw, not all warnings mean 'undefined behaviour'.
    This is what OP is doing exactly(idea).
    Except it has(should has?) defined behaviour?
    Code:
    void *p = NULL;
    p = &p;
    if( p == *(void**)p ) {
      printf("WOW\n");
    }
    Last edited by Bayint Naung; 07-19-2010 at 08:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM