By passing an address, you mean passing a reference. A difference is almost
purely syntactical (which in itself may be important when you are dealing
with templates). To a function accepting a pointer, you can pass a NULL
pointer which it can interpret in some meaningful way. There is no such
thing as NULL reference (and please, whoever reads this, don't start with
*(MyStruct*)0 - it's undefined behavior and has been discussed before).
Also, some people prefer passing a pointer for the following reason: when
you read a code written by somebody else, and you see a call that passes a
pointer, you expect the object to be modified by the called function. When
you see a call by reference, you don't know at a glance whether the method
accepts a reference or a value, and whether the object will be modified by
callee. Those people don't object to functions accepting const reference.
Other people prefer passing a reference because it saves some typing and
makes the code look "cleaner" with dots instead of arrows (which in itself
is a religious issue), and they argue that you need to inspect the headers
and learn what each function is doing anyway - you cannot expect C++ code to
be so self-documenting as to gather enough information just from looking at
the call statement.
All in all, it is a coding style issue, where there is no right or wrong,
but more of "works for me" or "I don't like it". I only hope I was careful
enough not to start a flame war here.
--
With best wishes,
Igor Tandetnik
Quote:
> This is probably a matter of choice, but can anyone tell me what the
> difference is (programmatically) between passing the address of a struct
or
> a pointer. They both do essentially the same, but why would you choose one
> instead of the other:
> //////////////////////////////////////
> #include <stdio.h>
> typedef struct _MyStruct {
> int nTest;
> } MyStruct;
> // Using the address
> void test(MyStruct &ms) {
> ms.nTest = 100;
> }
> // Using a pointer
> void test(MyStruct *pms) {
> pms->nTest = 100;
> }
> int main(int argc, char* argv[]) {
> MyStruct mystruct;
> test(mystruct);
> test(&mystruct);
> return 0;
> }
> //////////////////////////////////////