0
int num1 = 8;   //okay
int *pointer;   //okay
*pointer = &num1;   //NOT okay, compiler says (Error:a value of type int* cannot be
                    //assigned to an entity of type "int")

int num2 = 8;   //okay
int *pointer = &num2;   //okay

I am confused why the first part gives an error and the 2nd part doesnt, they look the same to me

  • 2
    Change it to `pointer = &num1`, as the type of `pointer` is `int*` and the type of `&num1` is also `int*` (while the type of `*pointer` is `int`). – barak manos Jul 16 '14 at 09:59
  • 5
    `*` and `&` have different meaning depending on the context. In a declaration they mean pointer type and reference type, in an expression they are the dereference and address of operators – bolov Jul 16 '14 at 09:59
  • `int *pointer = X;` means: `int *pointer; pointer = X;` – M.M Jul 16 '14 at 10:14

6 Answers6

6

In the assignment statement:

*pointer = &num1;   //NOT okay

*pointer is the value that pointer points to, of type int, and &num1 is the address of num1, of type int*. As the compiler says, you can't assign a pointer to an integer.

pointer = &num1 or *pointer = num1 would both be fine, depending on whether you want to modify the pointer itself, or the value it points to.

In the declaration:

int *pointer = &num2;   //okay

Despite the similar appearance to the assignment statement, this initialises pointer not *pointer. It declares pointer to be a pointer, of the same type int* as &num2.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

Remove the * in third line. It returns the value stored at position/address currently stored in variable pointer and interprets it as int.

RhinoDevel
  • 712
  • 1
  • 12
  • 25
0

The operator & gives a pointer to the variable over which is acting.
&num1 is a pointer to int (since num1 has type int) which points to the address of num1.

pablo1977
  • 4,281
  • 1
  • 15
  • 41
0

The error message is very clear: The expression *pointer is of type int and the expression &num1 is of type int*. Those two types are not compatible (you try to assign a pointer to a non-pointer).

When you use the dereference operator * on a pointer, you get what the pointer is pointing to, and if you use the address-of operator & you get the address of (i.e. pointer to) something.

To make pointer point to a variable, then just assign to it:

pointer = &num1;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0
int *pointer = &num2;

is the same as

int *pointer;
pointer = &num2;

not

int *pointer;
*pointer = &num2;
Martin G
  • 17,357
  • 9
  • 82
  • 98
0

The following is more of a general description rather than a direct answer to your question...

If you declare a variable of some type, then you can also declare another variable pointing to it.

For example:

int a;

int* b = &a;

There are two ways to "look at" variable b (that's what probably confuses most beginners):

  • You can consider b as a variable of type int*.

  • You can consider *b as a variable of type int.

Hence, some programmers would declare int* b, whereas others would declare int *b.

But the fact of the matter is that these two declarations are identical (the spaces are meaningless).

You can use either b as a pointer to an integer value, or *b as the actual pointed integer value.

You can read the pointed value (e.g., int c = *b) and write the pointed value (e.g., *b = 5).

barak manos
  • 29,648
  • 10
  • 62
  • 114