Thread: another question on pointers

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    another question on pointers

    i have come across **pointer_name before and as i decided to have a couple hours break from the poker game im writing i thought i would experiment.
    what is the difference between this
    Code:
    void double_it(int *p_number)
    {
        *p_number *= 2;
    }
    
    void add_three(int *p_num)
    {
        *p_num += 3;
        double_it(p_num);
    }
    int main()
    {
        int x = 1;
    
        add_three(&x);
        printf("x = %d\n", x);
        return 0;
    }
    and this
    Code:
    void double_it(int **p_number)
    {
        **p_number *= 2;
    }
    
    void add_three(int *p_num)
    {
        *p_num += 3;
        double_it(&p_num);
    }
    int main()
    {
        int x = 1;
    
        add_three(&x);
        printf("x = %d\n", x);
        return 0;
    }
    both give the correct answer of 8 (or at least the one i was expecting)

    i don't see the purpose of writing int **number when *number will do.
    coop

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not useful here. Useful when you need to assign to a pointer within a function such that it is changed in the caller, e.g., dynamic memory allocation. That is, when an out parameter is a pointer itself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    ok thanks i think that is probably where i saw it in an example of memory request

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. A question on pointers
    By Niels_M in forum C Programming
    Replies: 20
    Last Post: 08-27-2009, 08:05 AM
  3. A question about pointers in C
    By Kilua in forum C Programming
    Replies: 3
    Last Post: 06-05-2009, 02:33 AM
  4. Pointers to pointers question
    By mikahell in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2006, 12:54 PM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 11:13 PM

Tags for this Thread