Tor Rustad said:
The answer is that it depends.
No.
Let say T is a typedef for "int", then we have
const int var; /* declare read-only int */
int const var; /* declare read-only int */
which is exactly the same thing. "const" type-qualify
it's left, but the special case of "const" being first in
the declaration, it type-qualify it's right.
Yes.
Now let say T is a typedef for "int *". Then we have
these two cases:
int * const var; /* declare read-only int pointer */
const int *var; /* declare pointer to read-only int */
In the first case, the int pointer is const, while in the
letter case, int is const. This isn't the same thing!
No. The const qualifier applies to the entire typedef. Both variants
are interpreted as "int * const var".
The best way to avoid this confusion is to always place const on the
right of the type it qualifies.
If T were an unparanthesized macro, and not a typedef, your answer
would be correct, but I'm pretty sure that's not what the OP was
asked, and in all likelihood, the interviewer was following a script
and would not have understood a complete answer.
Here's a program which demonstrates this:
1: typedef int * T;
2: const T a;
3: T const b;
4: int main(int argc, char *argv[])
5: {
6: a = &argc;
7: *a = 1;
8: b = &argc;
9: *b = 1;
10: return 0;
11: }
As written, it results in the following errors:
const.c: In function `main':
const.c:6: error: assignment of read-only variable `a'
const.c:8: error: assignment of read-only variable `b'
if you replace the first line with
1: #define T int *
you get the following:
const.c: In function `main':
const.c:7: error: assignment of read-only location
const.c:8: error: assignment of read-only variable `b'
DES