LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A question about preference in C functions... (https://www.linuxquestions.org/questions/programming-9/a-question-about-preference-in-c-functions-4175447194/)

trist007 01-25-2013 11:42 AM

A question about preference in C functions...
 
Are there any benefits/downsides to passing an argument as a pointer to a function as opposed to passing the variable?

For example:

void doSomething(int x);

vs

void doSomething(int *x);

a4z 01-25-2013 11:46 AM

it is simply different,

passing a pointer means no copy of data,
and when doSomething changes x the caller has the new value

passing by value means that doSomething has a fresh and own copy of the passed data

trist007 01-25-2013 11:52 AM

I see now, great thanks.

millgates 01-25-2013 12:24 PM

I would add that, as a rule of thumb, if you want to pass a large object, such as a struct, to the function, it is much faster to just pass the pointer. If you want to pass a large object, but don't want that object changed by the function, or want to make it explicitly clear that the function will not change the object, use a const pointer.

trist007 01-25-2013 09:26 PM

Awesome thanks.


All times are GMT -5. The time now is 01:43 AM.