# V2009. Consider passing the 'Foo' argument as a pointer/reference to const.

This diagnostic rule was added at users' request.

The analyzer suggests that a function argument should be made a constant one.

This warning is generated in the following cases:

- The argument is an instance of a structure or a class which is passed into the function by reference but not modified inside the function body;
- The argument is a pointer to non-constant type, but it is used only for data reading.

This diagnostic may help you in code refactoring or preventing software errors in the future.

Consider the following sample:

    void foo(int *a)
    {
      int b = a[0] + a[1] + a[2];
      .... 'a' variable is not used anymore
    }

It is better to make the 'a' argument to point on a constant value. Therefore, it makes it clear that the argument is used for data reading only.

This is the fixed code:

    void foo(const int *a)
    {
      int b = a[0] + a[1] + a[2];
      .... 'a' variable is not used anymore
    }

Note. The analyzer may make mistakes when trying to figure out whether or not a variable is being modified inside the function body. If you have noticed an obvious false positive, please send us the corresponding code sample for us to study it.

Messages generated by the analyzer may sometimes seem pretty strange. Let's discuss one of these cases in detail:

    typedef struct tagPOINT {
        int  x, y;
    } POINT, *PPOINT;

    void foo(const PPOINT a, const PPOINT b) {
      a->x = 1;     // Data can be changed
      a = b;        // Compilation error
    }

The analyzer suggests rendering the type referenced by the pointer as constant one. It seems strange, since there is the keyword 'const' in the code. But 'const' actually indicates that the pointer is constant, while the objects that the pointers refer to are available for modification.

To make the objects themselves constant, we should do the following thing:

    ....
    typedef const POINT *CPPOINT;

    void foo(const CPPOINT a, const CPPOINT b) {
      a->x = 1;     // Compilation error
      a = b;        // Compilation error
    }