Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers: initialisation vs. declaration

I am a C++ noob and I am quite sure this is a stupid question, but I just do not quite understand why an error arises (does not arise) from the following code:

#include <iostream>
using namespace std;


int main() 
{
int a,*test; 

*test = &a;  // this error is clear to me, since an address cannot be 
             // asigned to an integer  


*(test = &a); // this works, which is also clear
return 0;
}

But why does this work too?

#include <iostream>
using namespace std;

int main() 
{
int a, *test= &a;  // Why no error here?, is this to be read as:
                   // *(test=&a),too? If this is the case, why is the 
                   // priority of * here lower than in the code above?

return 0;
}
like image 820
maxE Avatar asked Jun 17 '17 08:06

maxE


People also ask

What is pointer declaration and initialization?

While declaring/initializing the pointer variable, * indicates that the variable is a pointer. The address of any variable is given by preceding the variable name with Ampersand & . The pointer variable stores the address of a variable. The declaration int *a doesn't mean that a is going to contain an integer value.

What is the difference between declaration and initialisation?

Declaration tells the compiler about the existence of an entity in the program and its location. When you declare a variable, you should also initialize it. Initialization is the process of assigning a value to the Variable. Every programming language has its own method of initializing the variable.

Should pointers be initialized?

All pointers, when they are created, should be initialized to some value, even if it is only zero. A pointer whose value is zero is called a null pointer. Practice safe programming: Initialize your pointers!

What is the difference between declaration and initialisation of a variable?

Declarations specify the type followed by the name, and optionally the initialization. Initialization sets the variable to a new instance. It must be to a type that is compatible with the declaration type.


3 Answers

The fundamental difference between those two lines

*test= &a; // 1
int a, *test= &a; // 2

is that the first is an expression, consisting of operator calls with the known precedence rules:

       operator=
          /\
        /    \
      /        \
operator*    operator&
  |             | 
 test           a

whereas the second is a variable declaration and initialization, and equivalent to the declaration of int a; followed by:

   int*     test    =  &a
// ^^        ^^        ^^
//type    variable    expression giving
//          name        initial value

Neither operator* nor operator= is even used in the second line.

The meaning of the tokens * and = (and & as well as ,) is dependent on the context in which they appear: inside of an expression they stand for the corresponding operators, but in a declaration * usually appears as part of the type (meaning "pointer to") and = is used to mark the beginning of the (copy) initialization expression (, separates multiple declarations, & as "reference to" is also part of the type).

like image 193
Daniel Jour Avatar answered Oct 21 '22 23:10

Daniel Jour


int a, *test= &a;

is equivalent of:

int a;
int* test = &a;

and perfectly valid as you initialize test which has a type of pointer to int with an address of variable a which has a type of int.

like image 40
Edgar Rokjān Avatar answered Oct 21 '22 21:10

Edgar Rokjān


You're confusing two uses for *.

In your first example, you're using it to dereference a pointer. In the second example, you're using it to declare a "pointer to int".

So, when you use * in a declaration, it's there to say that you're declaring a pointer.

like image 2
haddie Avatar answered Oct 21 '22 23:10

haddie