Jump to content

Pointers/references

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
31 replies to this topic

#1
RobotGymnast

RobotGymnast

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
Hi, I was wondering what the difference between a pointer and a reference is. I generally references when passing classes or something with a structure.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
A pointer contains a memory address, which can be dereferenced to get access to the contents of the memory address it contains.
A reference is a second variable name for the same contents.
A pointer can have the memory address it contains changed, thus changing what it is referring to. A reference cannot change which variable it is a synonym for.
A pointer does not have to be initialized when it is declared. A reference has to be initialized when it is declared.

References are great in function calls where you want to be able to change a parameter, and they are slightly easier to work with than pointers (especially for people with an irrational fear of pointers).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
RobotGymnast

RobotGymnast

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
okay, thanks. I get it mostly now. I tend to do more direct manipulation, so more pointer stuff xD. thanks

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
My professor made me watch this ridiculous video. However, it does do a descent job on describing pointers: Binky Pointer Fun Video

#5
morefood2001

morefood2001

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,720 posts

John said:

My professor made me watch this ridiculous video. However, it does do a descent job on describing pointers: Binky Pointer Fun Video

Thats a great video, kind of childish, but it does get the crystal clear 'pointer' across ;)

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I know it is a great video. that is why I posted it. :) but thanks

#7
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
lol.

"Because it does not refer to a meaningful object, an attempt to dereference a null pointer usually causes a run-time error that, if unhandled, terminates the program immediately. In the case of C, execution halts with a segmentation fault because the literal address of NULL is never allocated to a running program."

Yeah, don't dereference a NULL pointer. Bad things happen.

#8
RobotGymnast

RobotGymnast

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
Yeah, you have to go like

if(pointer) delete pointer;

sort of annoying.

#9
keller

keller

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts

John said:

My professor made me watch this ridiculous video. However, it does do a descent job on describing pointers: Binky Pointer Fun Video

I watched the Ada version of that in my CS3 class, it does the job, but is really silly.

#10
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
Unless I'm mistaken, and I very well could be, deleting a NULL pointer is a noop command, and won't do anything useful, since NULL in C isn't actually allocated memory space... Correct me please if I'm wrong, I'm not 100%.

#11
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Yeah deleting a NULL pointer is pointless. It won't have any negative effects, but it's unnecessary.

#12
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
Well, actuuaaaaalllly. There's a lot of fun to be had with deleting NULL.
On older computers, where Ring protection and memory rights weren't available, it was possible to delete NULL. This would cause the computer to triple fault and die immediately :D