c++ i cant understand the difference between const char* and char* -


i know duplpicate belive me read alot of articles still cant understand diffrence im giving 2 examples.
1.

int strlen(const char* string) {    int = 0;    while (string[i] != '\0')    {        ++i;    }    return i; } 

2.

int strlen(char* string) {    int = 0;    while (string[i] != '\0')    {        ++i;    }    return i; } 

main:

int main()  {     char str[] = "hello";     cout << strlen(str) << endl; } 

the second work , wont errors while first wont.

case 1: can not change value of string, it's read-only. it's used prevent function changing value of parameter (principle of least privilege)

case 2: can change value of string.

also, check link comments.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -