C++ Programming/Operators/Pointers

Pointers, Operator *
The * operator is used when declaring pointer types but it is also used to get the variable pointed to by a pointer.

Pointers are important data types due to special characteristics. They may be used to indicate a variable without actually creating a variable of that type. Because they can be a difficult concept to understand, some special effort should be spent on understanding the power they give to programmers.

Pointers have a very descriptive name. Pointers variables only store memory addresses, usually the addresses of other variables. Essentially, they point to another variable's memory location, a reserved location on the computer memory. You can use a pointer to pass the location of a variable to a function, this enables the function's pointer to use the variable space, so that it can retrieve or modify its data. You can even have pointers to pointers, and pointers to pointers to pointers and so on and so forth.

Declaring
Pointers are declared by adding a * before the variable name in the declaration, as in the following example:

Watch out, though, because the * associates to the following declaration only:

You can also have multiple pointers chained together, as in the following example:

Assigning values
Everyone gets confused about pointers as assigning values to pointers may be a bit tricky, but if you know the basics, you can proceed more easily. By carefully going through the examples rather than a simple description, try to understand the points as they are presented to you.


 * Assigning values to pointers (non-char type)

The second statement uses " " the reference operator and  to tell the compiler this is a pointer variable and assign   variable's address to it. In the last statement, it outputs the value from the  variable by de-referencing the pointer using the   operator.


 * Assigning values to pointers (char type)

So as mentioned early, a pointer is a variable which stores the address of another variable, as you need to initialize an array because you can not directly assign values to it. You will need to use pointers directly or a pointer to array in a mixed context, to use pointers alone, examine the next example.

Remember you can't leave the pointer alone or initialize it as nullptr cause it will case an error. The compiler thinks it is as a memory address holder variable since you didn't point to anything and will try to assign values to it, that will cause an error since it does not point to anywhere.

Dereferencing
This is the * operator. It is used to get the variable pointed to by a pointer. It is also used when declaring pointer types.

When you have a pointer, you need some way to access the memory that it points to. When it is put in front of a pointer, it gives the variable pointed to. This is an lvalue, so you can assign values to it, or even initialize a reference from it.

Since the result of an & operator is a pointer, *&i is valid, though it has absolutely no effect.

Now, when you combine the * operator with classes, you may notice a problem. It has lower precedence than .! See the example:

The error happens because the compiler looks at p.num first ("." has higher precedence than "*") and because p does not have a member named num the compiler gives you an error. Using grouping symbols to change the precedence gets around this problem.

It would be very time-consuming to have to write (*p).num a lot, especially when you have a lot of classes. Imagine writing (*(*(*(*MyPointer).Member).SubMember).Value).WhatIWant! As a result, a special operator, <tt>-></tt>, exists. Instead of <tt>(*p).num</tt>, you can write <tt>p->num</tt>, which is completely identical for all purposes. Now you can write <tt>MyPointer->Member->SubMember->Value->WhatIWant</tt>. It's a lot easier on the brain!

Null pointer
The null pointer is a special status of pointers. It means that the pointer points to absolutely nothing. It is an error to attempt to dereference (using the <tt>*</tt> or <tt>-></tt> operators) a null pointer. A null pointer can be referred to using the constant zero, as in the following example:

Note that you can't assign a pointer to an integer, even if it's zero. It has to be the constant. The following code is an error:

There is an old macro, defined in the standard library, derived from the C language that inconsistently has evolved into #define NULL ((void *)0), this makes <tt>NULL</tt>, always equal to a null pointer value (essentially, 0).

Since a null pointer is 0, it will always compare to 0. Like an integer, if you use it in a true/false expression, it will return false if it is the null pointer, and true if it's anything else:

This program will output that the pointer is NULL, then that it isn't NULL twice, then again that it is.

Pointers and multidimensional arrays

 * Pointers and Multidimensional non-Char Arrays

A working knowledge of how to initialize two dimensional arrays, assign values to arrays, and return values from arrays is necessary. In depth information about arrays can be found in section 1.4.10.1.1 Arrays. However, when relevant to the understanding of pointers, arrays will be mentioned here, as well.


 * The main objects are:
 * 1) Assign Values to Multidimensional Pointers
 * 2) How to use Pointers with Multidimensional Arrays
 * 3) Return Values
 * 4) Initialize Pointers and Arrays
 * 5) How to Arrange Values in them

In non-Char Type you need to involve arrays with Pointers since Pointers treat char* type to in special way and other type to another way like only refer the address or get the address and get the value by indirect method.
 * 1) Assign Values to Multidimensional Pointers.

If you declare it like this way:

It will probably generate an error! Because pointers used in non-Char type only directly, in char types refer the address of another variable by assigning a variable first then you can get its (that assigned variable) value indirectly! There is another way instead it is You can use one of them to assign value to Array through the pointer to return values you can use either the appropriate Array or Pointer.

This is bit hard and even hard to remember so I suggest keep practicing until you get the spirit of Pointers only! You cannot use Pointers + Multidimensional Arrays with Char Type. Only for non-char type.
 * Pointers and multidimensional char arrays


 * Multidimensional pointer with char type

here the 5 in the first statement is the number of rows (no columns need to be specified in pointer it is only in Arrays) the next statement assigns another string to position 0 which is the position of first place of first statement. finally return the answer

In your system memory each memory block got an address so whenever you compile the code at the beginning all variable reserve some space in the memory but in Dynamic Memory Allocation it only reserve when it needed it means at execution time of that statement this allocates memory in your free space area(unused space) so it means if there is no space or no contiguous blocks then the compiler will generate and error message
 * Dynamic memory allocation

This is same as assign non-char 1 dimensional Array to Pointer
 * Dynamic memory allocation and pointer non-char type

The first statement's Lside(left side) declares an variable and Rside request a space for double type variable and allocate it in free space area in your memory. So next and so fourth you can see it increases the integer value that means *(pVal+0) pVal -> if this uses alone it will return the address corresponding to first memory block. (that used to store the 10) and 0 means move 0 block ahead but its 0 means do not move stay in current memory block, and you use parenthesis because + < * <  consider the priority so you need to use parenthesis avoid to calculating the * first
 * is called INDIRECT Operator which DE-REFERENCE THE Pointer and return the value corresponding to the memory block.

(Memory Block Address+steps)
 * -> De-reference.


 * Dynamic memory allocation and pointer char type

You can see this is the same as static memory declaration, in static declaration it goes:


 * Dynamic memory allocation and pointer non-char array type

Indirection operator ->
This pointer indirection operator is used to access a member of a class pointer.

Member dereferencing operator .*
This pointer-to-member dereferencing operator is used to access the variable associated with a specific class instance, given an appropriate pointer.

Member indirection operator ->*
This pointer-to-member indirection operator is used to access the variable associated with a class instance pointed to by one pointer, given another pointer-to-member that's appropriate.

Pointers to functions
When used to point to functions, pointers can be exceptionally powerful. A call can be made to a function anywhere in the program, knowing only what kinds of parameters it takes. Pointers to functions are used several times in the standard library, and provide a powerful system for other libraries which need to adapt to any sort of user code. This case is examined more in depth in the Functions Section of this book.