Thread: a pointer points itself

  1. #1
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275

    a pointer points itself

    Hi

    I wrote this code for fun, just wanted to see what will happen! I declared o pinter that holds its own address. But there is something wrong!
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char foo='f';	
    	char *pc;
    	char **ppc;
    
    	pc=&foo;
    
    	ppc=&pc;
    
    	printf("pc  -> %p\n",pc);
    	printf("&pc -> %p\n",&pc);
    	printf("ppc -> %p\n",ppc);
    	pc=&pc;
    	//ppc=&pc;
    	//pc=*ppc;
    	printf("pc  -> %p\n",pc);
    
    	return 0;
    }
    The code is compiled with "incompatible assignment" warning. But it works!

    But, i think the right way of doing this is to declare a **char and hold the address of *char in **char. But this does not work! When I uncomment the commented lines and leave out the pc = &pc; line, the code does not work!

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    Code:
    char foo='f';	
    char *pc;
    char **ppc;
    
    pc=&foo;
    ppc=&pc;
    
    printf("pc  -> %p\n",pc);
    printf("&pc -> %p\n",&pc);
    printf("ppc -> %p\n",ppc);
    
    /* pc=&pc; */
    ppc=&pc; 
    
    /* REPLACE 
        pc=*ppc; WITH : */
    pc = ppc;
    printf("pc  -> %p\n",pc);

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Well, pc holds an address information. ppc is also holds an address information. It is logical to equate them but one holds an address of a pointer and the other holds address of an ordinary variable.

    I think there should be a difference.If not so, what is the point of using a **char (just to create pointer arrays?)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void *p;
    p = &p;
    Behold the power of void.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In other words, you can assign any pointer to a void pointer, and retrieve the value later. Without compiler warnings.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No actually I mean, a void pointer is the only pointer that can point to itself.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Pointers" <-- crappy name
    By argv in forum General Discussions
    Replies: 64
    Last Post: 06-25-2009, 08:18 PM
  2. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  3. Replies: 4
    Last Post: 05-25-2008, 01:49 PM
  4. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  5. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM