Pointer/Address Question 
Author Message
 Pointer/Address Question

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;

Quote:
} MyStruct;

// Using the address
void test(MyStruct &ms) {
  ms.nTest = 100;

Quote:
}

// Using a pointer
void test(MyStruct *pms) {
  pms->nTest = 100;

Quote:
}

int main(int argc, char* argv[]) {
  MyStruct mystruct;

  test(mystruct);
  test(&mystruct);

  return 0;

Quote:
}

//////////////////////////////////////


Mon, 16 Feb 2004 04:31:34 GMT  
 Pointer/Address Question
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;
> }
> //////////////////////////////////////



Mon, 16 Feb 2004 05:11:31 GMT  
 Pointer/Address Question
One more issue: If you pass a pointer you can (internally)
change what the pointer points to, on a reference you can
not.

IMHO:
Use a pointer ONLY if:
    you will change what it is pointing to OR
    you need to accept NULL

Use a const (pointer or reference) if you are not going to
change the item [ Especially important so you can pass
const objects!!!!!]

Quote:
>-----Original Message-----
>By passing an address, you mean passing a reference. A

difference is almost
Quote:
>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
Quote:
>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
Quote:
>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

- Show quoted text -

Quote:
>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



>> 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

- Show quoted text -

Quote:
>> 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;
>> }
>> //////////////////////////////////////

>.



Mon, 16 Feb 2004 20:56:05 GMT  
 Pointer/Address Question
Not true.

#include <iostream>
using namespace std;

void f1(int *p) {*p = 1;}
void f2(int& i) {i = 2;}

int main()
{
    int i = 0;
    cout << i << endl;
    f1(&i);
    cout << i << endl;
    f2(i);
    cout << i << endl;
    return 0;

Quote:
}

Will print

0
1
2

Pointer and non-const reference are functionally equivalent.
--
With best wishes,
    Igor Tandetnik



Quote:
> One more issue: If you pass a pointer you can (internally)
> change what the pointer points to, on a reference you can
> not.

> IMHO:
> Use a pointer ONLY if:
>     you will change what it is pointing to OR
>     you need to accept NULL

> Use a const (pointer or reference) if you are not going to
> change the item [ Especially important so you can pass
> const objects!!!!!]

> >-----Original Message-----
> >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



> >> 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;
> >> }
> >> //////////////////////////////////////

> >.



Tue, 17 Feb 2004 01:44:30 GMT  
 Pointer/Address Question

Quote:
> 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.

For a lot of good reasons it is a very good idea to write "const correct"
code. One of the benefits is that you can easily tell [in] reference
parameters from [in/out] and [out] reference parameters.

Another reason for taking a reference parameter instead of a pointer is that
you can not have a null reference. This is useful because sometimes it is
important to force the caller to check for null. This is sort of the flip
side of using a pointer because you want null to have a special meaning.

Quote:
> 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.

I think it is deeper than a matter of syntactic style. For myself I find the
decision more often is driven by design issues.

Quote:
> --
> With best wishes,
>     Igor Tandetnik



> > 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;
> > }
> > //////////////////////////////////////



Tue, 17 Feb 2004 09:51:23 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Newbie pointer/address question

2. C Question - pointer & address-operator

3. question about NULL pointer and address 0x00000h

4. DOS, pointers & flat addressing Question

5. Question on pointer-to-pointer-to-pointer

6. address & pointer

7. Determining if a pointer has a bad address

8. passing address of a pointer as argument to a function

9. Pointer address and memory window problem

10. Changing pointer arg address from func?

11. confused with addressing / size of pointer

12. assign address to pointer array

 

 
Powered by phpBB® Forum Software