Thread: A simple pointer problem

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    A simple pointer problem

    I am just playing with c++ codes; why here count is printing 0, where I am expecting it to be 10, just want to count how many times the function is called itself.
    Code:
    using namespace std;
    
    void count_fn(int n, int *count)        {
            if(n==0)        {
                    return;
            }
            else    {
                    *count++;
                    n--;
                    count_fn(n, count);
            }
    }
    
    int main(int argc, char *argv[])
    {
            int *count = new int(0);
            count_fn(10, count);
            cout << endl <<"Count is" <<*count << endl;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look carefully at:
    Code:
    *count++;
    This dereferences count, and increments count. Rather, you want to increment the result of dereferencing count.

    Remember to delete what you new. In fact, you do not need dynamic memory allocation here: you can create count as an int, then pass its address to the initial call to count_fn. Better yet, you can make count a reference parameter.
    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
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by kapil1089thekin View Post
    I am just playing with c++ codes; why here count is printing 0, where I am expecting it to be 10, just want to count how many times the function is called itself.
    Code:
    using namespace std;
    
    void count_fn(int n, int *count)        {
            if(n==0)        {
                    return;
            }
            else    {
                    *count++;
                    n--;
                    count_fn(n, count);
            }
    }
    
    int main(int argc, char *argv[])
    {
            int *count = new int(0);
            count_fn(10, count);
            cout << endl <<"Count is" <<*count << endl;
    }
    "*count++" dereferences the pointer, then increments it (the pointer, that is - not the value); parenthesis around the dereference solves the problem. Also, don't forget to call the corresponding "delete".
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    134

    thanks

    I have solved it by giving the parentheses, but what difference it makes?
    but a problem arises again with the solution

    why the 1st code is working but the second code is throwing segmentation fault(core dump)?

    1st code:
    Code:
    using namespace std;
    
    void count_fn(int n, int *count)        {
            if(n==0)        {
                    return;
            }
            else    {
                    (*count)++;
                    n--;
                    count_fn(n, count);
            }
    }
    
    int main(int argc, char *argv[])
    {
            int count = 0;
            count_fn(10, &count);
            cout << endl <<"Count is" <<count << endl;
    }
    2nd code

    Code:
    #include<iostream>
    
    using namespace std;
    
    void count_fn(int n, int *count)        {
            if(n==0)        {
                    return;
            }
            else    {
                    (*count)++;
                    n--;
                    count_fn(n, count);
            }
    }
    
    int main(int argc, char *argv[])
    {
            int *count = 0;
            count_fn(10, count);
            cout << endl <<"Count is" <<*count << endl;
    }
    how delete is used?
    in the code which I posted previously, just to know the parameters used in delete and when it should be used?
    Last edited by kapil1089thekin; 08-05-2010 at 01:08 PM. Reason: Was a trivial fault in posting

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kapil1089thekin
    I have solved it by giving the parentheses, but what difference it makes?
    With the parentheses, you are incrementing the result of the dereferencing, not the pointer. Personally, I might write:
    Code:
    ++*count;
    Quote Originally Posted by kapil1089thekin
    why the 1st code is working but the second code is throwing segmentation fault(core dump)?
    Err... they look identical save for the header inclusion. Are you sure you posted the correct examples?

    Quote Originally Posted by kapil1089thekin
    how delete is used?
    With respect to your example in post #1:
    Code:
    // ...
    cout << endl <<"Count is" <<*count << endl;
    delete count;
    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

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    134

    Sorry for bad posting

    I corrected the codes/my post above...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kapil1089thekin
    I corrected the codes/my post above...
    Oh, then that is because count in main is a null pointer.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  2. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 04:50 AM
  3. printf output makes no sense. Is it a Pointer problem?
    By officedog in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 09:01 AM
  4. Simple Pointer Problem
    By Atlas24 in forum C++ Programming
    Replies: 7
    Last Post: 11-29-2005, 01:34 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

Tags for this Thread