self referencial pointers

Hi,

what is error in following program. want to refer pointer to itself.




#include<stdio.h>
#include<stdlib.h>

main()
{

int* p;
p = &p;


printf("memory address of pointer p: %d
",&p);
printf("value at p: %d
",p);
printf("value pointed by p: %d
",*p);

}


Comments

  • : Hi,
    :
    : what is error in following program. want to refer pointer to itself.
    :
    :
    :
    :
    : #include<stdio.h>
    : #include<stdlib.h>
    :
    : main()
    : {
    :
    : int* p;
    : p = &p;
    :
    :
    : printf("memory address of pointer p: %d
    ",&p);
    : printf("value at p: %d
    ",p);
    : printf("value pointed by p: %d
    ",*p);
    :
    : }
    :
    :

    :

    [blue]use %p to print the address of the pointer. Otherwise it should work, but I won't know what use you can make of a self-referencing pointer. Can't use it for anything.[/blue]


  • [b][red]This message was edited by tokoG at 2006-3-7 19:35:8[/red][/b][hr]

    [code]
    int* p;
    p = &p;
    [/code]

    Hi, I am just studying about the pointer right now too.
    I executed your code following Shaolin's advice to change %d to %p at the pointer's address.

    Wondered what is the value of p and *p represent for...? (different from the pointer's address)

    ps: Use [][/], not on this board. :)





  • : [b][red]This message was edited by tokoG at 2006-3-7 19:35:8[/red][/b][hr]
    :
    : [code]
    : int* p;
    : p = &p;
    : [/code]
    :
    : Hi, I am just studying about the pointer right now too.
    : I executed your code following Shaolin's advice to change %d to %p at the pointer's address.
    :
    : Wondered what is the value of p and *p represent for...? (different from the pointer's address)
    :
    : ps: Use [][/], not on this board. :)
    :
    [purple]
    currently i dont have a compiler to check the code, but i'm wondering why the above code will work!

    p = &p;

    the left side p is declared as a pointer that points to an integer, not a pointer to pointer to an integer!
    :-o
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • : : [b][red]This message was edited by tokoG at 2006-3-7 19:35:8[/red][/b][hr]
    : :
    : : [code]
    : : int* p;
    : : p = &p;
    : : [/code]
    : :
    : : Hi, I am just studying about the pointer right now too.
    : : I executed your code following Shaolin's advice to change %d to %p at the pointer's address.
    : :
    : : Wondered what is the value of p and *p represent for...? (different from the pointer's address)
    : :
    : : ps: Use [][/], not on this board. :)
    : :
    : [purple]
    : currently i dont have a compiler to check the code, but i'm wondering why the above code will work!
    :
    : p = &p;
    :
    : the left side p is declared as a pointer that points to an integer, not a pointer to pointer to an integer!
    : :-o
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    :
    [blue]C compiler will have no problems with that, but C++ will not compile it without a cast. It will work normally - the address of the location will be the contents of that location.[/blue]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories