5

I want foo() not to modify the array. So I declared array in foo() as const

If I compile this code, compiler is complaining:

#include <stdio.h>

void foo(const int arr[5][5])
{
    int i,j;
    for( i = 0; i < 5; i++)
    {   
        for( j = 0; j < 5; j++) 
        {       
            printf("%d\n", arr[i][j]);
        }       
    }   
}
int main()
{
    int val = 0;
    int i,j;
    int arr[5][5];
    for( i = 0; i < 5; i++)
    {   
        for( j = 0; j < 5; j++) 
        {       
            arr[i][j] = val++;
        }       
    }   
    foo(arr);
}

The warning is:

allocate.c: In function 'main':
allocate.c:26:9: warning: passing argument 1 of 'foo' from incompatible pointer type
     foo(arr);
         ^
allocate.c:3:6: note: expected 'const int (*)[5]' but argument is of type 'int (*)[5]'
 void foo(const int arr[5][5])
      ^

How else can I declare formal parameter as constant?

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
gjois
  • 2,025
  • 3
  • 18
  • 19
  • You don't want `foo()` to **be able** to modify the array? Because foo isn't modifying the array, it's just reading it. – moffeltje Apr 17 '15 at 07:27
  • 3
    http://stackoverflow.com/a/28062262/1606345 – David Ranieri Apr 17 '15 at 07:35
  • 1
    possible duplicate of [Pass a two dimensional array to a function of constant parameter](http://stackoverflow.com/questions/28062095/pass-a-two-dimensional-array-to-a-function-of-constant-parameter) – 2501 Apr 17 '15 at 08:52
  • @AlterMann, this post talks about issue, but not a fix. What should I do to fix this. Well I dont prefer adding compiler option as makefile is not supposed to be modified. – gjois Apr 17 '15 at 10:00
  • @user2763554, you have an answer – David Ranieri Apr 17 '15 at 10:29
  • @AlterMann, your answer was deleted? That actually solved the compilation problem. – gjois Apr 17 '15 at 13:11
  • 1
    @user2763554 I just read the list of updates to GCC, and it seems they fixed this issue [in the recently released gcc5.1](https://gcc.gnu.org/gcc-5/changes.html) – Bregalad Apr 20 '15 at 13:35
  • @Bregalad, thanks I read that. – gjois Apr 21 '15 at 08:54
  • @user2763554 So I just verified this with a prerelease build of gcc5, and yes, the warning is gone. – Bregalad Apr 22 '15 at 08:06

1 Answers1

-1

I assume that you do not want foo() to be able to modify the array in the interest of encapsulation.

As you are likely aware, in C arrays are passed into functions by reference. Because of this, any changes that foo() makes to the values array will be propagated back to the variable in main(). This is not in the interest of encapsulation.

const will not prevent foo() from modifying the array The C keyword const means that something is not modifiable. A const pointer cannot modify the address that it points to. The value(s) at that address can still be modified. In c, By default, you cannot assign a new pointer value to an array name. There is no need for const. Similar to a const pointer, the values in this array can still be modified. Encapsulation problem is not solved.

To encapsulate foo() from your main() function, put your array in a struct. Structs are passed by value and so foo() will receive a copy of the data. As it has a copy, foo() will not be able to alter the original data. Your functions are encapsulated.

Solution (that works!):

#include <stdio.h>

typedef struct
{
    int arr[5][5];
} stct;

void foo(const stct bar)
{
    int i,j;
    for( i = 0; i < 5; i++)
    {   
        for( j = 0; j < 5; j++) 
        {       
            printf("%d\n", bar.arr[i][j]);
        }       
    }   
}
int main()
{
    int val = 0;
    int i,j;
    stct bar;
    for( i = 0; i < 5; i++)
    {   
        for( j = 0; j < 5; j++) 
        {       
            bar.arr[i][j] = val++;
        }       
    }   
    foo(bar);
}

I put const in the foo() definition because you asked how you could declare the formal parameter as a constant. It works but is not needed.

void foo(stct bar)

Removing the const will not break the function.

  • `const int a[] = { 1, 2 }; a[0]=3;` -> error. `const` on an array or pointer means pointer to `const`. You're talking about something like `int *const p = &something`, where the pointer is a const, but the pointed-to location is not `const`. `a++;` -> error. `a[0]=0;` -> ok. – Peter Cordes Jul 07 '15 at 21:43