LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Passing input arguments by reference in C. (http://www.linuxquestions.org/questions/programming-9/passing-input-arguments-by-reference-in-c-898329/)

stf92 08-19-2011 02:19 PM

Passing input arguments by reference in C.
 
Hi:
Is it common practice in C to write functions with an input argument such that the caller passes a pointer as the input argument? Example:
Code:


int john(struct bridge *arg1)
{
FUNCTION BODY
}

struct bridge _arg1;

int_1 = john(&_arg1);


ta0kira 08-19-2011 02:22 PM

Quote:

Originally Posted by stf92 (Post 4447992)
Hi:
Is it common practice in C to write functions with an input argument such that the caller passes a pointer as the input argument?

This is the most common way to pass struct arguments in C, often with the const qualifier if the argument isn't going to be changed. This is apparent when you start using Linux/Unix C functions.
Kevin Barry

stf92 08-19-2011 02:42 PM

Thanks a lot.

MTK358 08-19-2011 06:15 PM

The reason for that is because it's much more efficient to copy a pointer than a large struct.

Another use for it is for the function to be able to modify variables passed as parameters.

stf92 08-19-2011 08:24 PM

Thanks a lot, MTK358.


All times are GMT -5. The time now is 08:53 AM.