Saksham Saksham - 1 year ago 146
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download