Difference between refference and pointers?

can anyone explain what is difference between refference and pointers. Is there just only differ its symbol? And how to use it. I try to google it but i did not understand. I just know about refference.

Hello Lee125,
If u have a snippet like:

#include <iostream>
using namespace std;
int main()
{
int var(2);
int * pointer;
pointer = &var; // store the address of var in the pointer
cout << pointer << '\n'; //print that address
cout << *pointer << '\n'; //print the values stored in the above address
}

First you will get on the screen the address where the integer 'var' is stored in memory and secondly you will get the value of integer var that is stored at that address.
Thanks clausserg. I'm understand now about pointers and refference. Thank you again my friend.
Yes pointers and references are two different things. Many things can be done with both pointers and references but if you have a choice I recommend using references.

A reference is like an alternative name for a variable. A reference can never be null. It always have to be initialized and you can't change what object it refers to later on. When using a reference you don't have to use the deference operator like you have with pointers. You just use it as if it were a normal variable.

1
2
3
4
5
6
7
8
9
10
int var = 2;
int& ref = var;
++ref;

// Prints value of var (3)
std::cout << ref;

// Prints address of var (It's impossible get the address
// of a reference because it's not an object)
std::cout << &ref; 

I think references are mostly useful when passing arguments to functions. It allows you to pass an object without creating a copy of the object but you can still prevent the function from modifying the object by using a const reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// The string is passed by reference to avoid creating a copy of the string.
void print_twice(const std::string& str)
{
	std::cout << str << std::endl;
	std::cout << str << std::endl;
}

int main()
{
	std::string str = "hello";

	// Note that we don't have to do anything special when passing the variable.
	print_twice(str); 
}
Last edited on
In other words,

When the source code has int foo; the compiler will generate binary code that allocates memory block for storing an integer number when the program is running. The compiler will also replace every spot, where the identifier foo is used, with binary code that reads/writes to that previously allocated memory block. The foo is a "variable".

When the source code has int & bar = foo; there will be no code that allocates memory. Instead, the compiler will keep in mind that whenever the code (in same scope) has either 'foo' or 'bar', the binary will read/write to the block that the foo represents. Peter did already state that the reference can have different qualifiers than the original identifier.

When the source code contains &foo, the binary will refer to the memory block of 'foo' and then do what the operator '&' does (in this context -- there is also operator Bitwise and), i.e return the address of the block.
Therefore, &foo == &bar, because bar is a reference to foo.


When the source code has int * gaz; the gaz is again a variable and code allocates memory for it during runtime. The type of gaz differs from type of foo. The gaz is a pointer and the foo is an int. The value stored by pointer variable is a memory address.

Therefore, gaz = &foo; will store into the memory block of gaz the address of the memory block of foo.

Pointer's type makes the compiler to memorize two additional things:
* Pointer types have dereference operator so that *gaz generates binary code that reads the address and then accesses the value in that address. I.e. after the previous assignment these are true: &foo == gaz and foo == *gaz
* You can write gaz + N (where N is integer) and the compiler will not simply add N to the address, but add sizeof(int)*N, because the unit of addresses bytes and in order to logically refer to next int (when ints are in consecutive memory blocks), you do need to advance sizeof(int) bytes.

As already said, you can change the address stored in pointer variable at any time and as many times as necessary. The address can even be nullptr, that represents invalid address, i.e. the pointer does not point to any address.

You can tell whether pointer has value nullptr or not, but you cannot tell whether that valid-looking address actually is of allocated memory block, or whether that block actually holds an entire array of elements.
Thanks to Peter87 and keskiverto.
Topic archived. No new replies allowed.