You are here:

C/constant pointer

Advertisement


Question
Is there any difference between *ptr and &ptr in following codes
code 1
------
   int x = 10;
   const int *ptr;
   ptr = &x;
Code 2
------
   int &ptr = x;

In code 1 ptr is an another variable than x pointing x.
Is ptr in code 2 an another variable than x, or, is another name to  same location x?

Answer
Hi, Ajay.

First, looking at your code, it appears to be mis-categorized.  Your second piece of code is creating a reference -- a C++ type.

References and pointers are remarkably similar.  They are both methods to access the same memory with different variables.  The differences are rather subtle.

First, a reference cannot be NULL.  This means that you are guaranteed that a references accesses some (supposedly) valid memory address.  This means that you do not have to validate a reference before using it, saving a branch statement (you do always check that your pointers aren't NULL, right? :)  With poor coding, it is possible to have a reference be bad, but it requires going from pointer to reference, which usually is not a good idea anyway.

Second, a reference need not be dereferenced before using it.  When you use a pointer, I'm sure you are used to putting a * in front of the pointer to dereference it or using the -> operator to access into pointers to structs.  With a reference, this is not required.  You can simply use the referenced variable as if it were the original variable, no dereferencing required.

A reference also cannot be made to reference a different variable once it has been created.  With pointers, you can reassign them to point to other variables at any time, but a reference, once created, always points to its original referenced variable.

Wikipedia has a pretty solid article on references for you to read:

http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29

If you are left with any questions, please don't hesitate to ask.  I'm here to help. :)

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.