Thread: function calling

  1. #1
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    function calling

    well, i dont know whether the code have some problem, if yes, pls give some explaination...

    [code]

    void function (STACK *ptr01, STACK *ptr02, char value)
    {
    // call to other function
    calling (*ptr01, *ptr02);
    ..
    ..
    ..
    }

    void calling (STACK **ptr01, STACK **ptr02)
    {
    ..
    ..
    // recursive call
    return (**ptr01, **ptr02, -1);
    ..
    ..
    }

    so is there some pointer problem??

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    void function (STACK *ptr01, STACK *ptr02, char value)
    {
    	/* call to other function */
    	calling (*ptr01, *ptr02);
    	/* code */
    }
    
    void calling (STACK **ptr01, STACK **ptr02)
    {
    	/* code */
    	/* recursive call */
    	return (**ptr01, **ptr02, -1);
            /* code */
    }
    Here is the syntax of code tags.
    And, unless I'm way off I'd think that return should be function.
    The world is waiting. I must leave you now.

  3. #3
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    ya, that's called recursion actually.

    but if you are return the value and call the function itself again, the pointer should different. for eg :

    when return the 3 value from the calling function and then call again the calling function. but the problem is, on the 2nd function, the function prototype should be

    >> void calling (STACK ***ptr01, STACK ***ptr02, char value)

    because ***ptr01 point to **ptr01, ***ptr02 point to **ptr02. that's mean i can't use recursion concept. so what should i do anyway??

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > so what should i do anyway??
    I don't know. What do you want to do?

    You never stated what your program was putting out..
    Give people something to hunt for and they'll help you.
    The world is waiting. I must leave you now.

  5. #5
    There shouldn't be any return on a VOID function.

    Code:
    void calling (STACK **ptr01, STACK **ptr02)
    {
    ..
    ..
    // recursive call
    calling(ptr01,ptr02);  //NOW IT SHOULD BE RECURSIVE, I CHANGED THIS
    ..
    ..
    }
    **Make sure you have some conditionals like IF statements in your recursive function otherwise it will LOOP forever.**
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  6. #6
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    Originally posted by OneStiffRod
    [B]There shouldn't be any return on a VOID function.

    Code:
    void calling (STACK **ptr01, STACK **ptr02)
    {
    ..
    ..
    // recursive call
    calling(ptr01,ptr02);  //NOW IT SHOULD BE RECURSIVE, I CHANGED THIS
    ..
    ..
    }
    can it be working? hmm.... i just knew that STACK **ptr01 and STACK **ptr02 that have been declared, the variable should be used is "**ptr01" and "**ptr02". but are these variable "ptr01" and "ptr02" can be used? i'll try. thank for your help OneStiffRod.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Shadow
    Code:
    void function (STACK *ptr01, STACK *ptr02, char value)
    {
    	/* call to other function */
    	calling (*ptr01, *ptr02);
    	/* code */
    }
    
    void calling (STACK **ptr01, STACK **ptr02)
    {
    	/* code */
    	/* recursive call */
    	return (**ptr01, **ptr02, -1);
            /* code */
    }
    Here is the syntax of code tags.
    And, unless I'm way off I'd think that return should be function.
    A few things here:
    Code:
    void function (STACK *ptr01, STACK *ptr02, char value)
    {
    	calling (*ptr01, *ptr02);
    First off, 'ptr01' is a pointer to type 'STACK'.
    Second, '*ptr01' is dereferencing that pointer. IE: Producing the STACK that it points at.

    You can all see where I'm headed with this...
    Code:
    void calling (STACK **ptr01, STACK **ptr02)
    {
    Here, we expect a pointer to a pointer to type STACK. Not a stack itself!

    As such, you really need:

    calling( &ptr01, ... )

    The return statement is entirely wrong. Why? Because it's a void function. It would technicly be legal to have a return statement:

    return 1,2,3,4,5,6,7;

    Assuming your fucntion actually returns an integer, this would be a valid return statement.

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

  8. #8
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    thanks guys, i got an ideal. i appreciate your all help.

  9. #9
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    quzah,

    according your comment , why should use this ???

    Code:
    calling( &ptr01, ... );
    it's not pass the address value of the pointer to the function.

    anyway, it should pass the address value into the function.
    the answer should be ...

    Code:
    calling( ptr01, ... );

  10. #10
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    is it possible to point to the pointer which is point to pointer which point to the pointer which point to the pointer ..... , by using recursion??

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Yes, Pointers can point to pointers which point to pointers and so on........ but too many levels will make the code too complicated.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    oh hammer,

    here's some bit explaination about the pointer

    ..... -> ***ptr01--> ** ptr01 -> *ptr01 -> ptr01

    this diagram shown at nested pointer.
    so the problem is if i tried to build the code with recursion concept, ppl said that it's impossible to do it by using recursion concept, coz if you wan to return the value is complicated. for eg :

    [code]
    int function (link *ptr)
    {
    ..
    ..
    ..
    ..
    return ( value + function(*ptr));
    }

    the 1st return to the function should be type the function protype like this ..
    Code:
    int function (link **ptr)
    so, what do you think , hammer?

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>return ( value + function(*ptr));
    You've dereferenced the pointer, so this isn't passing the pointer to the function, its passing link (whatever that is).

    >>int function (link **ptr)
    Now you've changed the function prototype from *ptr to **ptr. Make up your mind which it is.

    Maybe you should try and write a complete function and see what troubles you run into
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by beely
    quzah,

    according your comment , why should use this ???

    Code:
    calling( &ptr01, ... );
    it's not pass the address value of the pointer to the function.

    anyway, it should pass the address value into the function.
    the answer should be ...

    Code:
    calling( ptr01, ... );
    No it shouldn't. Look at your function prototyptes: Your function takes a pointer to a pointer. So if all you have is a pointer, then you need to pass it the address of that pointer so it equates to a pointer to a pointer.

    Just passing it the name of the variable would end up passing it a pointer, and that isn't what your prototype requires.

    Learn to understand the use of basic poniters correctly before using pointers to pointers.

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

  15. #15
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > Learn to understand the use of basic poniters
    http://www.google.com/search?hl=en&i...ng%3A+Pointers
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM