& vs. *

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

& vs. *

Post by hurstshifter »

Hey Guys,

So I have question which may sound a bit amateur but please bear with me (as I am an amateur :) ). I just started diving more deeply into OO programming with C++ and have recently been utilizing references. I've always utilized pointers and admittedly have had problems utilizing them the way I really wanted to in the past. Since learning about references, all those problems have pretty much disappeared. My main question is this...

What are the benefits of using a pointer to a variable rather than a call by reference?

It seems to me that they work in a similar manner (although I do understand the differences between the two). References appear to be a bit more efficient and just are cleaner overall when using them in a program. If you could provide some examples as to when it would be a better idea to use one over the other that would be awesome. I am ready to be ejumakated.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: & vs. *

Post by short »

One benefit of a pointer is that you can manipulate nearby memory addresses.

If you have:

int i = 5;
int * c = &i;
c++;


now c points to the memory address one place above (below?) where i is in memory.


edit: I know you can do this with stack allocated variables, but I am not so sure with heap-allocated variables if you can do this. It might be OS dependent, or language dependent, or not possible at all :lol:

edit: edited retarded error, lol.
Last edited by short on Fri Jul 10, 2009 1:42 pm, edited 1 time in total.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Netwatcher
Chaos Rift Junior
Chaos Rift Junior
Posts: 378
Joined: Sun Jun 07, 2009 2:49 am
Current Project: The Awesome Game (Actual title)
Favorite Gaming Platforms: Cabbage, Ground beef
Programming Language of Choice: C++
Location: Rehovot, Israel

Re: & vs. *

Post by Netwatcher »

pointers can point multiple objects in their lifetime, while reference can only ref one.

if you have:

decleration

Code: Select all

int i;
int *pti= &i;
int &rti = i;
to the pointer you can do

Code: Select all

pti++
and move freely in the memory...

assigning value

Code: Select all

*pti = 10;
vs

Code: Select all

rti = 10;
btw nice pun
hurtshifter wrote:So I have question which may sound a bit amateur
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655

blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: & vs. *

Post by K-Bal »

short0014 wrote: int i = 5;
int * c = i;
Hmm, ERROR ? ;)

Code: Select all

int i = 5;
int * c = &i;
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: & vs. *

Post by short »

lol, i think I was still hung over when I wrote that code this morning.

THanks :lol:
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: & vs. *

Post by hurstshifter »

I see. So references are like a one woman man, while pointers don't mind jumping around from address to address.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Netwatcher
Chaos Rift Junior
Chaos Rift Junior
Posts: 378
Joined: Sun Jun 07, 2009 2:49 am
Current Project: The Awesome Game (Actual title)
Favorite Gaming Platforms: Cabbage, Ground beef
Programming Language of Choice: C++
Location: Rehovot, Israel

Re: & vs. *

Post by Netwatcher »

hurstshifter wrote:I see. So references are like a one woman man, while pointers don't mind jumping around from address to address.
:lol:
hmm... more like you are the pimp and you tell the pointer what address he needs to be on
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655

blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: & vs. *

Post by programmerinprogress »

As vince the shamwow guy would say if he had a f****** clue about pointers and references!
with regular cloths you can only use them once (References), bim bam bow!, but with shamwow (pointers) you can clean up those spills eveyday! you're gonna love this! also, pointers are made in Germany whereas References are made in Russia, BIM BAM POW, BUY THIS NOW!
if you don't have a clue what i'm going on about, explore these videos at your leisure :lol:

Lesson 1: Basic Vince talk
http://www.youtube.com/watch?v=QwRISkyV_B8
Lesson 2: Introducing the Slap Chop
http://www.youtube.com/watch?v=rUbWjIKxrrs
Lesson 3: Advanced Vince offer rythms (Rap Chop)
http://www.youtube.com/watch?v=UWRyj5cH ... re=related

Enjoy :P
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
derbon
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Tue Jun 09, 2009 7:25 pm
Current Project: Big Nasty Enemies
Favorite Gaming Platforms: NES SMS snes PC N64 PS2 Vectrex The Arcade Machine
Programming Language of Choice: C++, Perl
Location: why shud i tell u u mite steal my tv
Contact:

Re: & vs. *

Post by derbon »

what??
i think the best paint program would be microsoft paint + paint.NET+graphics gale + Paint shop Pro 7 + photoshop CS3, it would be called Paint Gale Pro Shop.NET,

http://youtube.com/gonduda
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: & vs. *

Post by eatcomics »

I love rap chop... "You're gonna love my nuts!"

BTW that was the best analogy ever....
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: & vs. *

Post by Falco Girgis »

References are implemented at compile time as auto dereferenced pointers. Thinking of them in that light has always helped me.
User avatar
Netwatcher
Chaos Rift Junior
Chaos Rift Junior
Posts: 378
Joined: Sun Jun 07, 2009 2:49 am
Current Project: The Awesome Game (Actual title)
Favorite Gaming Platforms: Cabbage, Ground beef
Programming Language of Choice: C++
Location: Rehovot, Israel

Re: & vs. *

Post by Netwatcher »

programmerinprogress wrote:As vince the shamwow guy would say if he had a f****** clue about pointers and references!
BIM BAM POW, BUY THIS NOW!
OMG... Dr.seuss and the ShamWoW guy... ewww
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655

blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: & vs. *

Post by hurstshifter »

GyroVorbis wrote:References are implemented at compile time as auto dereferenced pointers. Thinking of them in that light has always helped me.
That definitely helps
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
Post Reply