Possible Duplicate:
What is the difference between char s[] and char *s in C?
There's a program:
#include<stdio.h>
int main()
{
char str[20] = "Hello";
char *const p=str;
*p='M';
printf("%s\n", str);
return 0;
}
This prints Mello as the answer.. But since p is a constant pointer, shouldn't it give an error?
It's a contant pointer, exactly. You can't change where it points. You can change what it points.
const char *p; // a pointer to const char
char * const p; // a const pointer to char
const char * const p; //combined...
The easiest way to memorize the syntax is to not memorize it at all. Just read the declaration from right to left :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With