const char double pointer
In this section, you are going to learn
What is
const char **q;
?What is
const char *const *q;
?What is
const char *const *const q;
?
Inorder to answer above questions, let us remember a very simple rule
Anything after
const
keyword CAN NOT be changed
Is it that simple ?
Yes. Let us see how do we apply above rule to answer the questions
Topics in this section,
Step 1 : Consider the statement
const char **q;
Step 2 : Remove all keywords after
const
const **q;
Step 3 : Remove everything before
const
const **q;
Step 4 : Remove assignment
const **q;
Step 5 : Now apply the rule. Anything after
const
keyword CAN NOT be changed
We see
**q
afterconst
Means,
**q
CAN NOT be changed again in next line
char a = 5;
char *p;
const char **q;
p = &a;
q = &p;
**q = 100; // --> This is invalid
Step 6 : Bonus point ! q, *q, **q are different
Means, q
and *q
can still be changed
char a = 5, b = 6;
char *x, *y;
const char **q;
x = &a;
q = &x; // --> This is valid
y = &b;
*q = y; // --> This is valid
printf("**q = %d\n", **q); // --> prints 6
There are two occurences of const keyword
Hence, let us apply the same rules two times
Step 1 : Consider the statement
const char *const *q;
Step 2 : Remove all keywords after first const
const **q;
Step 3 : Remove everything before first const
const **q;
Step 4 : Remove assignment
const **q;
Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed
**q CAN NOT be changed
Step 1 : Consider the statement
const char *const *q;
Step 2 : Remove all keywords after second const
const char *const *q;
Step 3 : Remove everything before second const
const *q;
Step 4 : Remove assignment
const *q;
Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed
*q CAN NOT be changed
**q, *q CAN NOT be changed
q can be changed
There are three occurences of const keyword
Hence, let us apply the same rules three times
Step 1 : Consider the statement
const char *const *const q;
Step 2 : Remove all keywords after first const
const **q;
Step 3 : Remove everything before first const
const **q;
Step 4 : Remove assignment
const **q;
Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed
**q CAN NOT be changed
Step 1 : Consider the statement
const char *const *const q;
Step 2 : Remove all keywords after second const
const char *const *q;
Step 3 : Remove everything before second const
const *q;
Step 4 : Remove assignment
const *q;
Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed
*q CAN NOT be changed
Step 1 : Consider the statement
const char *const *const q;
Step 2 : Remove all keywords after third const
const char *const *const q;
Step 3 : Remove everything before third const
const q;
Step 4 : Remove assignment
const q;
Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed
q CAN NOT be changed
**q, *q, q CAN NOT be changed
Statement |
Meaning |
---|---|
const char **q; |
|
const char *const *q; |
|
const char *const *const q; |
|
Current Module
Previous Module
Next Module
Other Modules