Thread: pass by reference by inner function in C

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    3

    Angry pass by reference by inner function in C

    I intend to change the value of count by function B(), but I failed...can't find the reason why...here's my code...
    Code:
    void B(int *count)
    {
         do{
            *count = *count + 1;
         }while(condition);
    }
    
    void A(int *count)
    {
        *count = 0;
        B(count);
    }
    
    int main()
    {
        int count;
        A(&count);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    How do you know, you never check the value of count after you call A. What is condition?

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    Quote Originally Posted by Subsonics View Post
    How do you know, you never check the value of count after you call A. What is condition?
    Well, the code I provided is a simplified version. I use printf() function to check in reality.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by shanwu View Post
    Well, the code I provided is a simplified version. I use printf() function to check in reality.
    Everything should be made as simple as possible but not simpler.

    Your code does not make sense, what is "condition"? Is it ever updated? Where is it declared?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shanwu
    Well, the code I provided is a simplified version. I use printf() function to check in reality.
    This is a test program based on your code:
    Code:
    #include <stdio.h>
    
    void B(int *count)
    {
        *count = *count + 1;
    }
     
    void A(int *count)
    {
        *count = 0;
        B(count);
    }
     
    int main(void)
    {
        int count;
        A(&count);
        printf("%d\n", count);
        return 0;
    }
    I ran it and it prints 1. Therefore, contrary to your assertion in post #1, there is no problem with your code where changing the value of count in function B is concerned.
    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
    Jun 2012
    Posts
    3
    Quote Originally Posted by laserlight View Post
    This is a test program based on your code:
    Code:
    #include <stdio.h>
    
    void B(int *count)
    {
        *count = *count + 1;
    }
     
    void A(int *count)
    {
        *count = 0;
        B(count);
    }
     
    int main(void)
    {
        int count;
        A(&count);
        printf("%d\n", count);
        return 0;
    }
    I ran it and it prints 1. Therefore, contrary to your assertion in post #1, there is no problem with your code where changing the value of count in function B is concerned.
    thank you very much. I will try to find other possible mistakes... thanks

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thus your problem is in 'condition' which you have left us guessing about so far.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-14-2012, 07:45 PM
  2. Knowing when function needs pass by reference.
    By bsdunx in forum C Programming
    Replies: 4
    Last Post: 08-29-2007, 12:38 PM
  3. Pass an array to a function by reference?
    By Loic in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2007, 11:44 PM
  4. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 01:06 PM
  5. Ask about function parameters and pass a reference.
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2002, 12:14 PM

Tags for this Thread