-2

I am learning some C++, and I came across pointers and addresses. However, in none of the materials, I could find a good explanation on when to use pointer, and when to use address. As I understand it is that when I use pointer, I POINT to address in memory, where some variable is stored. So for example:

int x = 5;
int k* = &x;

Which will mean that:

k represents x

When I change k, I also change value of x.

My question is as follows: when should I use pointer, and when should I use address? When I declare a function, should I use pointer, or address as a variable?

uksz
  • 18,239
  • 30
  • 94
  • 161
  • 1
    Pointer *is* address. `&x` means 'pointer at x' aka 'address of x'. – bereal Jan 23 '16 at 12:41
  • Oh. So when I change value of 'k' in my example, I dont change x? – uksz Jan 23 '16 at 12:43
  • I would not say that `k represents x` but that `k points to x` and it should be that `k` contains the address of `x` as its value (that's why it is pointing to it). – nbro Jan 23 '16 at 12:44
  • So guys, how can I change value of X having pointer K? – uksz Jan 23 '16 at 12:45
  • When you change value of `k` on its own, like `k++`, you don't change `x`, because `k` stores its address. *But* when you modify `*k`, as in `(*k)++` you dereference the pointer, so that modifies `x`, but not `k`. – bereal Jan 23 '16 at 12:46
  • 1
    You need to make the distinction between variables' addresses (in memory) and variables whose values are addresses (i.e., pointers). Memory is divided into chunks and a variable needs to be referenced somehow, that somehow is its address. `x` is not a pointer, so you should not want to do it at all. – nbro Jan 23 '16 at 12:47
  • Alright. Lets say that in declaration of my function I use address as a variable. In this case, can I modify x within the function? This leads my to my original question: when should I use pointer, and when address :) Sorry for the newbie question - I come from JavaScript, which seems very easy comapred to C++ and other basic languages :) – uksz Jan 23 '16 at 12:48
  • Using a pointer parameter makes it clear to the caller of a function that the value of the variable may be changed by the function. The user may, by contrast, not notice at all that a parameter of another function is declared a reference, and that the function potentially changes the value on the caller's side. From the caller's perspective a by-value and by-reference parameter look identical. S/he would have to consult the documentation to know. – Peter - Reinstate Monica Jan 23 '16 at 16:55

3 Answers3

3

You have a typo there. It should be:

int x = 5;
int *k = &x;

It should be read as: k points to x. Or if you insist on the "represent" word: *k represents x.

The & operator takes any variable as argument and returns its address (pointer). The * gets a pointer (address) as argument and returns the value stored there. As such they are opposite operations: &*k is the same as k and likewise *&x is just like x. You can look at these expression this way:

x //an integer
k //pointer to integer
&x //pointer to x
*k //integer pointed to by k
*&x //integer pointed to by a pointer to x, that is x
&*k //pointer to the integer pointed to by k, that is k

Note that operator & can only be used on variables, as expressions in general do not have addresses, only variables do (well, and temporaries, but that is another matter). For example, this expression is invalid:

&(x + 1) //error!

The funniest thing with these operator is how a pointer is declared:

int *k;

You might think that it would be better written as int &k, but that's not how declarators are read in C++. Instead, that declaration should be read as:

int (*k);

that is, *k is an integer, so it results that k is a pointer to an integer.

Now, the declaration with initialization is weirder:

int *k = &x;

should actually be read as:

int (*k);
k = &x;

That is, you declare *k as being an integer, thus, k is a pointer-to-integer. And then you initialize k (the pointer) with the address of x.

Actually you can create pointers of any type, even pointers to pointers, pointers to pointers to pointers... but note that this syntax is illegal:

int x;
int **p = &&x; //error, &x is not a variable

but this is valid:

int x;
int *k = &x;
int **p = &k;
rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • This is really good answer. However, I still am wondering when should I use pointer and address when I declare a function - or it totally doesnt matter? – uksz Jan 23 '16 at 16:06
  • @uksz: You are asking when to pass to the function the address of a variable instead of a copy of the value. That's useful when the value is large and you want to avoid the copy, when you want to pass an array of objects (you pass a pointer to the first one), when the function has to modify the value, when you want to make the value optional (there is a `NULL` pointer)... – rodrigo Jan 23 '16 at 20:07
1

If a variable stores an address of a memory location, it is considered a pointer. So an int * for example stores the address of an int-variable. By using the dereference-operator *, you can access the memory location at the address of the pointer and assign to it.

To get the address of a variable, you use the &-operator. This is what your example does. To assign to the memory-location where x is stored, you would use the derefence-operator again like this: *k = 0;

Note that the derefence-operator and the * to express a pointer type are two different things. * and & are each other's inverse (overloading aside) operators, while int * is a type.

In C++ in particular, if the & is used together with a type, the type of the variable is a reference-type, e.g. const string &s. Just like the pointer-type this is different from the address-of operator. Reference-types do not need to be derefenced using *, but will direcly modify the memory location they reference.

midor
  • 5,487
  • 2
  • 23
  • 52
0

Pointer operator (*) is used whenever you want to point to a variable.

Address operator (&) is used whenever you point to the memory address of a variable.

Abhishek Rathore
  • 1,016
  • 1
  • 11
  • 13