Next Generation Emulation banner

Difference between reference and pointers.

1336 Views 6 Replies 5 Participants Last post by  Syed Fawad
I tried to search the web but I am kinda having difficulty in finding some "basic" difference between these two in layman terms.

POINTER:

int A;
int *pA;
pA = &A;

REFERENCE:

int A;
int &rA = A;

So what?

A is the name of the address of 2 bytes reserved in memory for integer type.
pA is the pointer which contains the address of A.
So what would rA be?
1 - 1 of 7 Posts
A reference is the address at which a variable is located in memory. A pointer is used to store this reference.

Code:
int a = 5;         /* store 5 in int a */
int *ptra = &a;    /* store reference to a in pointer to int ptra
int b = *ptra;     /* dereference pointer a, store in int b */
printf("%d", b);
This example code would output "5".

The obvious advantage of passing variables by reference, rather than by value, is that only one word of data is passed, rather than a, possibly long, piece of data.
1 - 1 of 7 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top