A simple pointer problem

This is a discussion on A simple pointer problem within the C++ Programming forums, part of the General Programming Boards category; I am just playing with c++ codes; why here count is printing 0, where I am expecting it to be ...

  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
    21,598
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,699
    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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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
    21,598
    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;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,598
    Quote Originally Posted by kapil1089thekin
    I corrected the codes/my post above...
    Oh, then that is because count in main is a null pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:42 AM
  2. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 03: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, 12:34 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

Tags for this Thread


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