Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: pointers... (http://www.velocityreviews.com/forums/t313801-re-pointers.html)

Hans-Bernhard Broeker 06-25-2003 02:49 AM

Re: pointers...
 
In comp.lang.c.moderated geo <geometrikal@hhoottmmaaiill> wrote:

[...]

> What is the best programming practice:


> void function(struct random_struct * r) {
> .......
> }


> or:


> void function(struct random_struct r) {
> .......
> }


As with all good "either ... or" questions, the answer is either "it
depends", or "none of these".

These two ways of of passing a struct to a function differ not only in
"programming practice", but also in their actual effect. The first
will let changes to *r done inside the function propagate to the
calling function, the second won't.

If that's not what the function is supposed to be doing, be sure to qualify
the struct as 'const':

void function (const struct random_struct * const p){
/* ... */
}

thereby making both the pointer itself and the struct it points to
read-only for function().

That settled, the remaining issue is indeed one of good practice, but
still the answer is "it depends". To be precise, it depends on the
size of the struct, and the platform this will be used on, whether you
can afford passing a copy of the struct instead of just the pointer.

--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net


All times are GMT. The time now is 12:29 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57