Saksham Saksham - 3 months ago 38
C Question

Assigning string literal to pointer to const char

May I know why this assignment is possible?

char const *c = "Hello";
c = "there!";


Isn't it pointing to a location whose contents cannot be modified. As far as I am able to make out, it is creating another object and making c point to that. Is it true.

Any other highlight upon this would be appreciated.

Answer

Yes, const applies to whatever is to the left of it (unless there is nothing to the left of it).

You're just pointing c to a different string.

Comments