OT: Functions and Pointers

# Thread: OT: Functions and Pointers

1. Senior Member
Join Date
Dec 1969
Posts
11,334

## OT: Functions and Pointers

I know it&#039;s off topic, but it&#039;s Friday :)<BR><BR>I cannot see the difference between these two statements in C++. Is there something I&#039;m missing?<BR><BR>FUNCTION 1:<BR><BR>int somefunction(int* myNumber)<BR>{<BR> *myNumber = 10;<BR> return *myNumber;<BR>}<BR><BR>and call it by<BR><BR>int x = 1;<BR><BR>int s = somefunction(&x);<BR><BR>FUNCTION 2:<BR><BR>int somefunction(int& myNumber)<BR>{<BR> myNumber = 10;<BR> return myNumber;<BR>}<BR><BR>and call it<BR><BR>int x = 1;<BR><BR>int s = somefunction(x);<BR><BR><BR>These do the same things, don&#039;t they?

2. Senior Member
Join Date
Dec 1969
Posts
2,092

## RE: OT: Functions and Pointers

I&#039;m no c++ expert so that&#039;s why I fowarded your post to c guy here at work he told me he&#039;s gonna have to test it but has far as he knows he&#039;s saying that it&#039;s only the nomenclature that&#039;s different.<BR><BR>He also said that in Microsoft&#039;s code they rarely use the "&" the more often use the * <BR><BR>but like I said I&#039;m no expert and so is he :(<BR>

3. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: OT: Functions and Pointers

Thanks VLince...<BR><BR>Well, the test is silly, since they&#039;re not &#039;real&#039; functions. Obviously returning the value when you modify the variable directly is silly. Guess I should&#039;ve made them void returns.<BR><BR>I was more leaning towards this:<BR><BR>void somefunction(int& MyNumber)<BR><BR>Seems like less typing :)<BR><BR>The question is if they&#039;re the same thing, just different ways of writing it.

4. Senior Member
Join Date
Dec 1969
Posts
2,092

## RE: OT: Functions and Pointers

Back from playing a Hacky game, I saw him in the hall way, and he told me he&#039;d be sending me an email with the results. He did mention that there was a difference but which ? I can&#039;t say :)

5. Senior Member
Join Date
Dec 1969
Posts
2,092

## I know you know but here goes nothing...

---BEGIN EMAIL---<BR>First form: Pointer. The function use a pointer (created on the stack) to manipulate data and/or address of the passed object. This kind of parameters exists in both C++ and C.<BR><BR>The other form is called a reference. The parameter behave the same way as the object passed to the function. The indirection operator (*) is useless. The address of the object however can be obtained with the address-of operation (&). See this diagram for complete details:<BR><BR><BR>Pointer (int* myNumber)<BR>-----------------------------------<BR><BR>&myNumber = address-of pointer<BR><BR>myNumber = address-of object pointed<BR><BR>*myNumber = value pointed (indirection)<BR><BR><BR>Reference (int& myNumber)<BR>----------------------------------------<BR><BR>&myNumber = address-of object pointed<BR>myNumber = value pointed<BR>*myNumber = illegal<BR><BR><BR>Both do the same action but in different syntax.<BR>---END EMAIL---<BR>

6. Senior Member
Join Date
Dec 1969
Posts
11,334

## Ah

I had a feeling they were the same thing with different syntax... just checkin&#039;.<BR><BR>He&#039;s wrong about that being created on the free store though... I never &#039;new&#039; ed the var. Though I could be wrong also... who am I to doubt anybody at this point.<BR><BR>Thanks VLince... tell your buddy thanks also.<BR><BR>End email? English? I thought you were in the French part of Montreal?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•