Thread: Pointer

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    3

    Question Pointer

    Here, &p gives address of pointer. So, it act as double pointer?
    Is it true?


    int *p;
    p = &p;
    printf("%u %u %u ",p, *p, &p);


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It might help if you posted something that will compile.
    Code:
    int i;
    int *p = &i;  // points to the int
    int **p = &p; // points to the pointer, which in turn points to the int
    printf("%p %p %p\n", (void*)&i, (void*)p, (void*)(*pp) );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Certainly, my code complies in VS 2016 without problem.

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Salem you have changed the entire code. I never asked for double pointer declaration and initialization (int **p = &p).
    int *p;
    p = &p;
    printf ("%d %d %d", p, &p, *p);
    Finely works in Visual Studio 2013 and GCC

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Shree Ge View Post
    Certainly, my code complies in VS 2016 without problem.
    Quote Originally Posted by Shree Ge View Post
    Salem you have changed the entire code. I never asked for double pointer declaration and initialization (int **p = &p).
    int *p;
    p = &p;
    printf ("%d %d %d", p, &p, *p);
    Finely works in Visual Studio 2013 and GCC
    You say it works fine, but are you getting any warnings? You're not providing a complete compilable example for us to reference.

    For what it's worth, your example is wrong. "p" is a pointer to int, but you're treating it as if it were a pointer to a pointer.

    Are you asking if a pointer can point to itself? I can't find anything in the rules that doesn't allow this (though I haven't looked very hard, nor can I find a good reason to do so).

    What's even worse is when you try to dereference an uninitialized *p, which is undefined behavior.
    Last edited by Matticus; 12-30-2016 at 11:38 PM. Reason: Added Link

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Oh really....
    Code:
    $ cat main.c
    #include <stdio.h>
     
    int main()
    {
      int *p;
      p = &p;
      printf ("%d %d %d", p, &p, *p);
      return 0;
    }
    $ gcc -Wall main.c
    main.c: In function ‘main’:
    main.c:6:5: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
       p = &p;
         ^
    main.c:7:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
       printf ("%d %d %d", p, &p, *p);
               ^
    main.c:7:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int **’ [-Wformat=]
    I've no idea what your question is, if you keep posting junk like p = &p

    There is no such thing in C where T and T* are the same thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-18-2015, 03:13 PM
  2. Replies: 8
    Last Post: 03-01-2015, 12:39 AM
  3. Replies: 3
    Last Post: 03-27-2014, 12:10 AM
  4. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM

Tags for this Thread