triple pointers *** question

This is a discussion on triple pointers *** question within the C Programming forums, part of the General Programming Boards category; Hello, I'm writing a program that will be utilizing triple pointers but for some reason my test program isn't working ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    triple pointers *** question

    Hello, I'm writing a program that will be utilizing triple pointers but for some reason my test program isn't working as it should:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
           char *tmp;
           char *ptr;
           char **ptr2;
           char ***ptr3;
    
           ptr = malloc(2);
           *ptr = 5;
           tmp = ptr + 1;
           *tmp = 6;
           *ptr2 = ptr;
           **ptr3 = ptr2;
    
           printf("%d\n", ***ptr3);
           printf("%d\n", ***(ptr3 + 1);
    
           return 0;
    }
    The result should be:
    5
    6

    Instead I get a segmentation fault.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by someprogr View Post
    Code:
    *ptr2 = ptr;
    **ptr3 = ptr2;
    how about referencing the pointer before pointing to an invalid location.
    Last edited by itCbitC; 01-06-2009 at 02:18 PM.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Well how about actually using an integer, since you are printing with %d.

    Also, I don't think ptr3 actually contains an address you can dereference. But I do know that you can probably do this without triple indirection. What are you really doing?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's because you have ptr3 pointing to the memory allocated as ptr - which holds the value 5 and 6 and two more bytes of "random" rubbish. So when the computer reads that memory it gets some address like 0x77180605 or 0x05061877 [which one you get depends on the order the processor reads memory to form a 32-bit value] (that's just a random guess, it can be any 65535 other values there). That doesn't look, to me, like an address that you would be able to read a pointer from, so it will fail there.

    You may want to do this:
    Code:
    ...
      ptr2 = &ptr;
      ptr3 = &ptr2;
    ...
    Not only will that compile without warning [something I can almost guarantee that your original code produces when you compile it], but it will also work correectly.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by itCbitC View Post
    how about (de)referencing the pointer before pointing to an invalid location.
    well from my understanding *ptr2 has to point to a char * pointer type and so i pointed it at ptr because thats exactly what it is: a char * pointer.

    Same for ptr2... **ptr2 points to a 32-bit address (in a 32-bit system) or four byte char *. Hence it should equal ptr2.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Pointer assignment assigns the value though, not the address of the actual pointer. For that you have to use the address-of (&) operator.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by someprogr View Post
    well from my understanding *ptr2 has to point to a char * pointer type and so i pointed it at ptr because thats exactly what it is: a char * pointer.

    Same for ptr2... **ptr2 points to a 32-bit address (in a 32-bit system) or four byte char *. Hence it should equal ptr2.
    but what about plain old ptr2 and ptr3, where do you think they should point to?? see matsp's reply.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by someprogr View Post
    well from my understanding *ptr2 has to point to a char * pointer type and so i pointed it at ptr because thats exactly what it is: a char * pointer.

    Same for ptr2... **ptr2 points to a 32-bit address (in a 32-bit system) or four byte char *. Hence it should equal ptr2.
    But a triple pointer is:
    Code:
    *(*(*ptr))
    Each of those () will produce a 32-bit (or whatever size a pointer is) read before we get to the outermost memory access that reads a byte.

    Oh, and your (ptr3 + 1), the +1 is in the wrong place - it should be *((**ptr3)+1).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    A pointer is a variable that contains a memory address, not values. That's the first rule.
    A pointer is a variable that "points" to a value. The type of that value is part of the pointer's type.
    For example: int* p.
    p is a pointer that points to int. The pointer's actual type is int*.
    So if a pointer is a variable, and all variables store their contents in memory, then it must be possible to take the address of a pointer too, yes? Correct. If we want a pointer to pointer to a pointer, we get... a pointer to int*, or the actual type int**.
    And so it continues.
    Since a pointer is a variable, by default, it contains garbage (!!). So it must point to something valid first. This is your responsibility as a programmer.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 05:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21