pointers


How does the expression, “*pointer++” evaluate?

*pointer++ will increment the position of the pointer first but it will return the value that was pointed before the position of the pointer changed.

*pointer++ is equivalent to *(pointer++). This happens because the postfix ++ and -- operators have higher precedence than the indirection (dereference).
You can read more about the precedence order at this very helpful article at Wikipedia: https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

To increment the value pointed to by pointer, use (*pointer)++.

To increment the position of the pointer and return the value that is pointed after the position of the pointer changed use *++pointer.

Examples

The following example will increment the position of the pointer but return the value that was originally pointed.
By the end of the following block, pointer will point to position 1 and the value variable will have the value 11.


const unsigned int values[] = {11, 12, 14, 18};
const unsigned int *pointer = values;
const unsigned int value = *pointer++;

The following example will increment the position of the pointer and return the value that the new position is pointing to.
By the end of the following block, pointer will point to position 1 and the value variable will have the value 12.


const unsigned int values[] = {11, 12, 14, 18};
const unsigned int *pointer = values;
const unsigned int value = *++pointer;


C/C++: How NOT to define multiple pointers

When defining pointers in C/C++ you should be careful on how you use the * characters. If you try to define multiple pointers on the same line and you do not add the * character in front of each variable, then the results will not be what you would expect. In the following examples we added the * to the data type definition, hoping that all variables would become pointers of that data type. Unfortunately, as the compiler points out later on while making the comparisons, only the first variable in each line becomes a pointer of the data type.

Wrong Examples

C Source Code:

#include <stdio.h>

int main()
{
    int* a, b;
    int *c, d;
    int * e, f;

    a == b;
    c == d;
    e == f;

    return 0;
}

Compiler Output:

$ gcc -o main *.c                                                                                          
main.c: In function 'main':                                                                                      
main.c:9:7: warning: comparison between pointer and integer                                                      
     a == b;                                                                                                     
       ^                                                                                                         
main.c:10:7: warning: comparison between pointer and integer                                                     
     c == d;                                                                                                     
       ^                                                                                                         
main.c:11:7: warning: comparison between pointer and integer                                                     
     e == f;                                                                                                     
       ^                                                                                                         

C++ Source Code:

#include <iostream>
using namespace std;

int main()
{
    int* a, b;
    int *c, d;
    int * e, f;

    a == b;
    c == d;
    e == f;

   return 0;
}

Compiler Output:

$ g++ -std=c++11 *.cpp -o main

main.cpp: In function ‘int main()’:
main.cpp:10:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     a == b;
          ^
main.cpp:11:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     c == d;
          ^
main.cpp:12:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     e == f;
          ^

Correct Examples

#include <stdio.h>

int main()
{
    int* a, * b;
    int *c, *d;
    int * e, * f;

    a == b;
    c == d;
    e == f;

    return 0;
}
#include <iostream>
using namespace std;

int main()
{
    int* a, * b;
    int *c, *d;
    int * e, * f;

    a == b;
    c == d;
    e == f;

   return 0;
}