-1

My first programming language learned was Actionscript, and I'm having a hard time understanding pointers and references.

What is the purpose of pointers when I can just use references? I read one use case for pointers is when giving a large variable to another function instead of copying it, but can't I just add a & in front so it acts like a pointer?

(I know how knowing/passing the memory address of something is helpful, but I don't understand why when there is &)

EDIT: I mean references as using &

apscience
  • 7,033
  • 11
  • 55
  • 89
  • I think you are talking about C++ (not C) as you compare pointers to references. References are a C++-feature; C has pointers only. – Flinsch Dec 15 '11 at 07:59
  • http://stackoverflow.com/questions/92001/what-is-the-real-difference-between-pointers-and-references –  Dec 15 '11 at 08:02

8 Answers8

2

There is no difference in a pointer and the &-Construction. A pointer is a variable which stores the value of &...

int a;
int *b = &a;
Nikodemus RIP
  • 1,369
  • 13
  • 20
1

If you haven't already, I recommend checking out the Binking Pointer Fun videos. They are highly informative in a gentle manner ... and quite amusing.


C does not have references. Languages like Java and ActionScript -- although ECMAScript, from which AS is based, does not mention references in the specification -- use the term "reference" to essentially mean "a pointer [value] that always refers to a valid object".

C does not have references. It has pointers. Pointers are much more basic and primitive and there is no guarantee that they point to a valid object.

Also note, that Java and ActionScript (and C) are all call-by-value (although see call-by-object-sharing for a better higher-level term when "passing objects" in Java or when "passing pointers" in C). In Java when you pass an object to a method the underlying implementation passes the reference value representing the object: in is very much like passing a pointer value to a function in C.

Happy coding.

  • @gladoscc Perhaps you could post some code (edit the main question)? I believe you mean `&` to mean the "address-of" operator, but a real example would clarify your question more. –  Dec 15 '11 at 08:02
1

C does not have references, they're a C++ feature. C has pointers which are essentially special variables which hold a memory address. When you dereference a pointer using the * operator then you're telling the compiler you want the value at the address stored in the pointer. The & operator means 'address of', so &i means the address of the variable i.

// create an integer variable holding the value 1
int i = 1;

// create a pointer to an integer and set the value of the pointer to the address of i
int * ptr = &i

// print the value at the address pointed to by ptr by dereferencing the pointer
printf("%i\n", *ptr); // prints 1

You can think of a pointer as a reference in that it points to an instance (in the majority of use cases) of a value. Of course, a pointer can point to any memory address which fits in the number of bits the pointer is composed of, which is what makes C so easy to shoot yourself in the foot.

Matt Lacey
  • 8,227
  • 35
  • 58
  • "it points to an instance (theoretically) of a value" -- in C, a pointer is not required to point to an instance of a value ... it can legally point inside a value, or even to the address just past a value. – Jim Balter Dec 15 '11 at 09:06
  • Hence the "(theoretically)" - but maybe that's not the best word to use. Typically most developers (including those who need to ask about the `&` operator) are going to want their pointers pointing at an instance of a structure or other variable. – Matt Lacey Dec 15 '11 at 09:16
  • Indeed it's the wrong word to use because the only "theory" of C is that defined by the Standard. What you are talking about is practice, not theory. – Jim Balter Dec 15 '11 at 09:27
1

a reference in C is a pointer, so when you pass by reference you give the address of the argument to the function.

void foo(mybigStruct *p);

mybigstruct S;

foo( &S ); // passing S by reference which is the same as

mybigstruct *p = &S;

foo( p ); // p points to address of S

as opposed to passing by value which just copies the argument to the function, passing by reference(ptr) allows the function to modify its argument since the function can directly access the original place at a particular memory address where the passed data resides i.e. S can be modified by foo in the above example.

AndersK
  • 35,813
  • 6
  • 60
  • 86
0

Within a function, you can increment/decrement a pointer argument; you can't change a reference argument. The latter prevents you from shooting yourself in the foot. I suspect the reference (and Java, and C#) were invented when Management realized that large numbers of their programmers were never going to get pointers right, and were doomed to an eternity of chasing memory leaks, using freed pointer bugs, etc. in their applications.

References also are a bit easier to read ('ref.field' instead of 'ptr->field'). For this reason I use references instead of pointers wherever possible.

If a function is allocating memory, a double-pointer seems more natural:

void foo(char **ptr)
{   *ptr = (char *)malloc(1000);
}

The reference declaration seems backwards:

void bar(char * &ptr)    // I would have expected char &*ptr ?
{    ptr = (char *)malloc(1000);
}
Pierre
  • 4,114
  • 2
  • 34
  • 39
0

Technically, pointers and references are the same in C++ (C has pointers only). A conceptual difference is that references guarantee to represent a valid object while pointers could represent invalid objects (null-pointers). Whether to use a pointer or a reference is very situational and also a matter of taste.

Flinsch
  • 4,296
  • 1
  • 20
  • 29
  • First, this is not a C++ question so answering in terms of C++ is off point. Second, pointers and references are not the same in C++, technically or otherwise. I suggest that you read http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29 – Jim Balter Dec 15 '11 at 09:03
0

A big difference between pointers and references is that pointers can be null. This can be used for additional information e.g. in a function call. A reference always needs a "backup" to which it "refers". So a pointer would be used if the argument of a function can be null, otherwise a reference would suit better.

Another thing is the return value of a function. Theoretically you can return a reference to a function-local variable but it is most likely that your program will crash because the function is removed from the stack and the reference will have no more its "backup" available. In this case a pointer to a new allocated memory has to be used.

Another use case are pointers to pointers e.g. as a argument to a function. The function can then allocate data at the original pointer.

In fact pointers in c is a huge topic which pitfalls and best uses are best learned and understood by practice.

IO.Nathan
  • 21
  • 4
  • "So a pointer would be used if the argument of a function can be null, otherwise a reference would suit better." -- This is a C question, not C++. C doesn't have references. – Jim Balter Dec 15 '11 at 08:59
0

What is the purpose of pointers when I can just use references? I read one use case for pointers is when giving a large variable to another function instead of copying it, but can't I just add a & in front so it acts like a pointer?

Ok, here's a "large variable":

struct foo {
   int a[10000];
} biggie;

and here we pass it to a function by adding & in front of it:

some_function(&biggie);

but what does the function look like?

void some_function(struct foo* ptr)
{
    ...
}

See, &biggie is a literal pointer that is the address of biggie, and ptr is a variable pointer that can point to any struct foo.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66