C++ Cplusplus pointers and arrays syntax

Post date: Aug 13, 2017 2:14:29 PM

Spaces (whitespaces) before or after * and & are not relevant, and don't change the meaning.

Pointer or reference of variable = address of variable

Differences

1. references always point to the same variable, can't be changed. Pointers can be changed to point to a different variable.

2. different syntax: a reference is like an alias of the variable, no * needed.

while declaring the pointer or reference, the data type of the variable has to be known.

int myvariable = 34;
int* mypointer = &myvariable; // address of the int variable or array. The * here indicates that we're declaring a pointer (not to be confused with the * below). The & indicates that we mean the adress of the variable, not the value.
int& myreference = myvariable // myreference and myvariable are now essentially the same thing. my reference is an alias of myvariable.
myvariable // value
&myvariable // address
*myvariable // convert a pointer to the value. does not make sense, since myvariable is not a pointer
myreference // value
&myrefernce // address
*myreference // does not make sense, since myvariable is not a pointer
mypointer // address
*mypointer // value
&mypointer // does not make sense, since mypointer is an address already

Dereference operator: value of variable of pointer:

*mypointer // the value of the variable, where the pointer points to

has nothing directly to do with the reference of a variable, see above.

Create array

int * pointers[][11] // type of variable - is in this case an array of pointers. The last dimension (in this case 11) needs to be specified.

Arrays are actually pointers to the first value of the array. So the above is a pointer to a pointer.

See also http://www.cplusplus.com/doc/tutorial/pointers/

Pass by reference or pointer

pass the address( reference or pointer) of variable to a function, so the function can change the value of the variable directly.

see http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

difference between pointer and reference: http://www.cplusplus.com/articles/z6vU7k9E/

declare pass by reference:

void myfunction (int & internal_function_variable)
{
    internal_function_variable ... // value of the variable
}

declare pass by pointer:

void myfunction (int * internal_function_variable)
{
    internal_function_variable ... // address of the variable
    *internal_function_variable ... // value of the variable
}

call:

myfunction (myvariable);

The same could be achieved with global variables.

The difference is that with passing variables from function to function, only the explicitly allowed functions can access the variable.

Pass variable to function by value (copy value, keep original variable unchanged)

declare:

void myfunction (int internal_function_variable)

call:

myfunction (myvariable);

The value of myvariable is copied to internal_function_variable. If the function modifies internal_function_variable, myvariable stays unchanged