Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By Subsonics

pass by reference by inner function in C

This is a discussion on pass by reference by inner function in C within the C Programming forums, part of the General Programming Boards category; I intend to change the value of count by function B(), but I failed...can't find the reason why...here's my code... ...

  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
    21,888
    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.
    Salem likes this.
    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
    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.
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-14-2012, 06: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, 12:06 PM
  5. Ask about function parameters and pass a reference.
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2002, 11:14 AM

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