Passing Parameters

This is a discussion on Passing Parameters within the C++ Programming forums, part of the General Programming Boards category; Ok, this is kinda confusing me, and what i thought should work, didn't. Lets say i have the following code: ...

  1. #1
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128

    Question Passing Parameters

    Ok, this is kinda confusing me, and what i thought should work, didn't. Lets say i have the following code:

    Code:
     
    int variable;
    
    void function1(int *variable)
    {
         // do something
         function2(int *variable);
    }
    
    void function2(int variable)
    {
    
    }
    Of course, this wouldnt work because your passing a pointer to a non-pointer parameter. And you cant call
    Code:
    function2(int variable);
    as that is not passing in the parameter correctly which was recieved in function 1.
    I thought that i could get it to work by using:
    Code:
    function2(int &(*variable));
    That way, it is passing the value of the pointer, hence what im after. But this doesnt work

    So how can i call function2 inside function1? Do i need to change the structure of function2 to pass a pointer to it? If so, then how would i call it in function 1?
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    choices

    You can;

    Pass to function 2 by pointer;
    Code:
    #include<iostream>
    
    void function2(int*);//forward declaration
    
    void function1(int *variable)
    {
         // do something
         function2(variable);
    }
    
    void function2(int *variable)
    {
    	std::cout << *variable << std::endl;
    }
    
    int main()
    {
      
    	int variable = 10;
    
    	function1(&variable);
    
    	return 0;
    }
    or by value
    Code:
    #include<iostream>
    
    void function2(int);//forward declaration
    
    void function1(int *variable)
    {
         // do something
         function2(*variable);
    }
    
    void function2(int variable)
    {
    	std::cout << variable << std::endl;
    }
    
    int main()
    {
      
    	int variable = 10;
    
    	function1(&variable);
    
    	return 0;
    }
    You can also bring references into it....
    Code:
    #include<iostream>
    
    void function2(int&);//forward declaration
    
    void function1(int& variable)
    {
         // do something
         function2(variable);
    }
    
    void function2(int& variable)
    {
    	std::cout << variable << std::endl;
    }
    
    int main()
    {
      
    	int variable = 10;
    
    	function1(variable);
    
    	return 0;
    }
    choices..choices....

  3. #3
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    Ah, well thanks. I like choices That helps a lot, though i actually found the problem in my code to be:

    Code:
    *variable++;
    which should be
    Code:
    (*variable)++;
    But now i know how to pass the variables correctly, i found it Thanks.
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  2. Replies: 7
    Last Post: 04-19-2006, 12:17 PM
  3. Question about passing & receiving parameters
    By TCB in forum C Programming
    Replies: 9
    Last Post: 04-11-2006, 07:08 AM
  4. Passing parameters to modal dialogs
    By Halloko in forum Windows Programming
    Replies: 2
    Last Post: 10-11-2005, 08:15 AM
  5. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM

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