0

What benefit does it have to have the keyword const on the function below.

BOOL8 CheckSimilarity(const Name_t NameOne, const Name_t NameTwo)
  • Would it affect function in any way if we didn't put keyword const?

I always thought that you will have to pass pointers for variables and compare with pointers in the function so it goes to memory location where the variable is stored and compares the variables themselves as in swap function as in K&R?

typedef UINT8 Name_t[5]

Log_t* Log(Name_t Name)
{
   Log_t *point2Log = Log1;
   while (point2Log < Log1)
   {
      if (CheckSimilarity(Name, point2Log->Name))
      {
         return point2Log;
      }
      point2Log++;
   }
   return NULL;
}


BOOL8 CheckSimilarity(const Name_t NameOne, const Name_t NameTwo)
{
    UINT8 count;
    for (count=0; count<5; count++)
    {
        if (NameOne[count] != NameTwo[count])
        {
            return FALSE;
        }
    }
    return TRUE;
}
newb7777
  • 517
  • 5
  • 22
  • It has no effect in the prototype and is a waste of space there – M.M Jun 14 '17 at 04:32
  • 2
    const before function argument use to assure not any effect on that argument. – EsmaeelE Jun 14 '17 at 04:32
  • BTW the `while` loop is never entered – M.M Jun 14 '17 at 04:33
  • And Compiler not re-allocate Memory location for argument that is const. You know in every function call, we have a new copy that is local scope on its function body and suppose argument is int array[5000] this is huge volume of Memory, in situation we do not need to change array element using leading const is very good to save unnecessary memory consumption and speed up program. – EsmaeelE Jun 14 '17 at 04:38
  • 3
    @EsmaeelE even if the argument is `int array[5000]` a new copy is not made. Arrays are passed by reference always! – Ajay Brahmakshatriya Jun 14 '17 at 04:42
  • @Ajay: are you sure? can show me reference say something about that? – EsmaeelE Jun 14 '17 at 04:44
  • @Ajay: "Arrays are passed by reference always!" Same statement is true for Struct. – newb7777 Jun 14 '17 at 04:51
  • Yes, for structs it is true. I was going to add that the comment can be changed to give a better example of structs rather than int[5000] – Ajay Brahmakshatriya Jun 14 '17 at 04:53
  • Possible duplicate of [What kind of optimization does const offer in C/C++? (if any)](https://stackoverflow.com/questions/27466642/what-kind-of-optimization-does-const-offer-in-c-c-if-any) – phuclv Jun 14 '17 at 04:54
  • [Does const-correctness give the compiler more room for optimization?](https://stackoverflow.com/q/6313730/995714), [do “const” declarations help the compiler (GCC) produce faster code?](https://stackoverflow.com/q/20693136/995714) – phuclv Jun 14 '17 at 04:55
  • @EsmaeelE see [§6.7.6.3/P7](http://port70.net/~nsz/c/c11/n1570.html#6.7.6.3p7) clearly states that arrays are adjusted to pointers of same type. This is C99 btw. – Ajay Brahmakshatriya Jun 14 '17 at 05:20

1 Answers1

3

In the case of arguments, it tells the compiler that the function will not modify its arguments. This in turn might enable the compiler to do some shortcuts or optimizations that it otherwise might not have done.

It is also something that your fellow programmers can read, and know that they can call the function without worrying about possible side effects to the contents of e.g. arrays.


And talking about programmers and what they can read, it seems that you have defined Name_t to be an alias for a pointer. Please don't do that, it makes the code harder to read and follow and maintain.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    About the second half, I believe the `const` is on the argument itself and not the pointed type. So I think the arrays are still subject to be changed. – Ajay Brahmakshatriya Jun 14 '17 at 04:32
  • @AjayBrahmakshatriya For e.g. `const Name_t NameOne` it says that `NameOne` is a `Name_t` that is constant. If `Name_t` is an alias for a pointer, then it means that it points to constant (read-only) data. It's like a variable pointing to a constant string (like a string literal), where you have `const char *` as the type. – Some programmer dude Jun 14 '17 at 04:34
  • 2
    @Someprogrammerdude no, it would be equivalent to `char * const`. (If `Name_t` is a typedef, that is... `#define Name_t char *` is different of course) – M.M Jun 14 '17 at 04:35
  • @Someprogrammerdude I think the const would be on the pointer and not the `char` – Ajay Brahmakshatriya Jun 14 '17 at 04:36
  • @Ajay, point taken. What if `typedef UINT8 Name_t[5]` how does it play out? – newb7777 Jun 14 '17 at 04:47
  • 1
    @newb7777 then the array contents become constant. – Ajay Brahmakshatriya Jun 14 '17 at 05:07