Thread: passing variables between functions

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    passing variables between functions

    I have a function that its prototype is:
    Code:
    float AverageOfValues(TNODE *root, int *amm_of_nodes);
    I know that, unlike in C++, in C you cant pass variables by-refrence (&var),
    My question is how whould I return the variable amm_of_nodes
    recursively without using the "&".?
    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Is there something wrong with the way you have it now?
    Sure there are other ways, how contrived do you want the answer to be?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    The function has to be recursive and when I call the function from inside the function i'll have to write:
    avgL=AverageOfValues(root->left, &num_of_nodesL);
    and from what I understand you can't do that in C?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know what a pointer is? References are pointers except you don't have to put a shiny * next to them to dereference them. (Oh, and you can't have a null reference.)

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    2
    I don't quite get what you're trying to do but, by doing:

    int *amm_of_nodes

    in your functions parameter, you're passing a pointer to the int you passed in, in the calling function. Thus, you could change that value in AverageOfValues() without having to return anything.

    But, for various reasons which only you know, you might still want to return the variable *amm_of_nodes, The thing is - it's not possible. But you can do something similar to it but IMO (In My Opinion) it's pointless and a bit of extra tricky-ness code. Either do it like I said above or just return the value *num_of_nodes points to and not a pointer to some variable like I'm about to show.

    To return a pointer which holds *amm_of_nodes value (aforementioned 'extra tricky-ness code'):

    Code:
    int* AverageOfValues(TNODE *root, int *amm_of_nodes)
    {
    
    static int val = *amm_of_nodes;
    // do your stuff here
    return &val;
    
    }
    There's a problem with using static here though. Because, It will ONLY get initialized once and live for the duration of your program. Thus: "return &val;" will always be returning the same address of val everytime you call it. If this does'nt suit your needs, then, you have another option. Remove the static keyword on val and make it a pointer to an int and Use malloc to Dynamically allocate memory for val; Just don't forget to use free(); for every malloc calll you make.

    As a final note; You can't return the variable amm_of_nodes, only it's value can be returned.. (because it won't be destroyed after leaving AverageOfValues() since it's a variable that was declared in the calling function. Only the pointer int *amm_of_nodes gets destroyed) ..which is an address to some variable declared in your calling function.
    Last edited by n00bster; 05-07-2005 at 04:50 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I think you're making this way more complicated than it needs to be.

    Consider the following snippets
    Code:
    float AverageOfValues(TNODE *root, int *amm_of_nodes);
    
    int main ( ) {
      TNODE *tree = makeATree();
      int numNodes = 0;
      float average = AverageOfValues(tree, &numNodes);
      return 0;
    }
    
    float AverageOfValues(TNODE *root, int *amm_of_nodes) {
      if ( root ) {
        *amm_of_nodes++;
      }
      if ( root && root->left ) {
        return AverageOfValues( root->left, amm_of_nodes );
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    Thanks for the help, it helped me undestand alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing functions between files
    By Duskan in forum C Programming
    Replies: 9
    Last Post: 04-17-2007, 07:44 AM
  2. Variables and Functions
    By Hitetsu in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2006, 08:01 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM