0

I wonder does C/C++ allow one to use "const int **" in function call?

Suppose I have a matrix, which can be accessed by pointer to pointer. When I want to use this matrix, and forbid modification of any value in this matrix, can I do "func(const int **mat)"? I have tried this, but when I do something like this "a = mat[0][0]", there is error message.

I wonder if it is allowed to use "const int **" or what is the correct way to do that?

Thanks!

  • 1
    What's the error message? – R Sahu Jul 24 '15 at 22:09
  • 1
    See http://stackoverflow.com/a/1143272/4107809 – mattm Jul 24 '15 at 22:10
  • 1
    possible duplicate of [What is the difference between const int\*, const int \* const, and int const \*?](http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const) – mattm Jul 24 '15 at 22:11
  • There is a helpful website for the type definitions: http://cdecl.org/. Try inputting `int ** const var`. – rkapl Jul 25 '15 at 12:57
  • I suspect your problem is not the type you're passing. Am I correct in assuming `a = mat[0][0]` is called inside `func`'s function body? Also, what are you passing to `func` - how is it declared and initialised? – Sigve Kolbeinson Jul 25 '15 at 15:02

1 Answers1

1

const modifier binds to the token to the left of it. If it is placed all to the left, it is moved one place to the right. So when you write const int **, it means int const **: the integer finally pointed to is constant, but the pointer may be manipulated. This is probably exactly what you want.

So if you are getting an error, it is probably because your a doesn't refer to an int const (or -- const int), but just an int.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • But what you want to achieve is that the `mat[0][0]` is constant but that `a` can be assigned to. So assigning a const to an int is fine, but assigning an int to a const is flagged, – Paul Ogilvie Jul 24 '15 at 22:48