A pointer to pointer (**p) question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jthep
    New Member
    • Oct 2007
    • 34

    A pointer to pointer (**p) question

    If I have a pointer say int **pa that stores the address to another pointer int *pb, pa would be storing the address of pb which contains an address to lets say int c = 10. Picking an arbitrary address for pb to be 5009 and c to be 2331. Can someone verify for me if I have the following values correct:

    pa = 5009
    pb = 2331
    *pb = 10

    would **pa = 2331? If so, how can I obtain the value of c through pa?

    I'm trying to write a function that deletes a record from a linked list by name:
    struct record
    {
    char name[25];
    char address[80];
    int yearofbirth;
    char telno[15];
    struct record *next;
    };

    From what I read in my textbook and online from the net...the list will start with a struct record pointer that stores the address of the first record. So when I start my delete function, I would pass the address of that pointer into the function in order to alter the next field should the first record is the one that needed to be deleted. Therefore the param should be struct record **start. The value in start would be the address to the pointer that stores the address of the first record. Then the value in the address of the record pointer can be obtain through **start. How can I obtain the value whose address is stored in record pointer through start?

    Oh well, not so quick question...lol.

    EDIT: One way I thought of was to declare another struct record pointer and define it to be **start (value, address to first record) Then should it turns out that the first record contains the name to be deleted, the following code should modify the list:

    struct record *nextRecord = ** start
    nextRecord = nextRecord.next
    clear record whose address is in **start from heap
    **start = nextRecord

    Is this the correct syntax? Is there another way to do this? Would it just be better to start the list with a struct record instead of a struct record pointer?

    Thanks,
    jthep
  • jthep
    New Member
    • Oct 2007
    • 34

    #2
    I think that another way to delete the first struct record can go as follows too..

    struct record *nextRecord = ** start
    **start = **start.next
    clear record whose address is in nextRecord from heap

    Comment

    • mattmao
      New Member
      • Aug 2007
      • 121

      #3
      This question is taken from, could be, the Linked List Chapter.

      You need to record the address of the head pointer to handle extreme cases, say, what if you want to delete the first element in this Linked List?

      Search for any references regarding "C Linked List" and you definitely would see a bunch of code examples show you what is the proper way of doing these modifications.

      Generally speaking, a pointer to pointer is the same as a pointer to variable.
      It contains the address of a pointer, which is pointing at a variable's address in memory.

      you can dereference once to get the address of that pointer to variable and twice to get the content of the variable.


      Code:
      #include <stdio.h>
      
      int main()
      {
          int a = 100;
          int * pty = &a;
          int ** pty2 = & pty;
      
          printf("This is a %d\n", a);
          printf("This is what the pty contains %d\n", *pty);
          printf("This is what the pty2 contains %d\n", **pty2);
      }
      Result:
      Code:
      bash-3.2$ gcc doublepointer.c
      bash-3.2$ ./a.out
      This is a 100
      This is what the pty contains 100
      This is what the pty2 contains 100
      bash-3.2$

      Comment

      • jthep
        New Member
        • Oct 2007
        • 34

        #4
        Originally posted by mattmao
        This question is taken from, could be, the Linked List Chapter.

        You need to record the address of the head pointer to handle extreme cases, say, what if you want to delete the first element in this Linked List?

        Search for any references regarding "C Linked List" and you definitely would see a bunch of code examples show you what is the proper way of doing these modifications.

        Generally speaking, a pointer to pointer is the same as a pointer to variable.
        It contains the address of a pointer, which is pointing at a variable's address in memory.

        you can dereference once to get the address of that pointer to variable and twice to get the content of the variable.


        Code:
        #include <stdio.h>
        
        int main()
        {
            int a = 100;
            int * pty = &a;
            int ** pty2 = & pty;
        
            printf("This is a %d\n", a);
            printf("This is what the pty contains %d\n", *pty);
            printf("This is what the pty2 contains %d\n", **pty2);
        }
        Result:
        Code:
        bash-3.2$ gcc doublepointer.c
        bash-3.2$ ./a.out
        This is a 100
        This is what the pty contains 100
        This is what the pty2 contains 100
        bash-3.2$
        ooooh okay...so now I remember what my professor told me. Taking your example, I can view dereferencing as "cell whose address in pty2 is equal to address of pty1" and that would give the the double dereferencing to be "cell whose address in pty2 is equal to ('cell whose address in pty1 is equal to 100')" which is 100. Thank you.

        Comment

        • mattmao
          New Member
          • Aug 2007
          • 121

          #5
          Originally posted by jthep
          ooooh okay...so now I remember what my professor told me. Taking your example, I can view dereferencing as "cell whose address in pty2 is equal to address of pty1" and that would give the the double dereferencing to be "cell whose address in pty2 is equal to ('cell whose address in pty1 is equal to 100')" which is 100. Thank you.
          My demo code might not be the proper way of demonstrating the use of pointer to pointer. Anyway if you want to see a professional demonstration upon this issue, find a reference book. I found your saying was a little bit of "not to the point"...

          Anyway,

          Code:
          pty2 = &pty;
                       pty = *pty2;

          Think of pty as a variable rather than a pointer, that might give you a clearer idea.

          Comment

          Working...