I have declared a pointer to const char and incremented the pointer, it is not giving any issue.
Code:
int main()
{
const char * ptr;
char *c = "ABC";
ptr = c;
ptr++;
return 0;
}
I have used typedef for declaring a pointer to const char as shown below, but it is giving error during compilation. It seems it is treating pointer as constant pointer.
Code:
typedef char *P;
int main()
{
const P ptr;
char *c = "ABC";
ptr = c;
ptr++;
return 0;
}
Error during compilation:
Code:
test.c: In function ‘main’:
test.c:10:2: error: assignment of read-only variable ‘ptr’
test.c:11:2: error: increment of read-only variable ‘ptr’
I think typedef is used for defining typedefs, in this case ptr is treated as constant pointer, not a pointer to constant char.
Code:
typedef char *P;
const P ptr;
Is there any limitations with typedef in C.