What is the difference between line 9 and line 10
Line 9 means, str1 is a pointer to constant character string which means that once you assigned it a string, You can't change content of the string with pointer str1.
i.e. if I uncomment line #11, it will give us compilation error.
Line 10 means, str2 is constant pointer to character string which means that pointer is constant not string i.e. You can not make str2 to point to some another pointer.
i.e. if I uncomment line #12, it will give us compilation error.
But following lines are valid.
One thumb of rule that will help you to remember is that you start reading from right to left. e.g.
If you read line #9, you should read it as str1 is pointer to character string that is constant
If you read line #10, you should read it as str2 is constant pointer which points to character.
Now isn't it easy to understand?
Now let us see what does following code mean?
Line #5 means that str3 is constant pointer that points to character string which is constant.
If I uncomment line #6 and #7 it will give us compilation error because neither you can't change content of string nor content of pointer i.e. pointer can't point to a different string.
Line #9 means that str4 is a pointer that points to constant character string. Hence str4 and str1 in first code snippet are equivalent. There are two ways to write same statement.
Similarly str5 and str3 are equivalent.
In nut shell, if you remember the thumb rule(i.e. read from right to left) You don't have to memorize anything.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> #include <string.h> using namespace std; int main() { char* str = new char[8]; strcpy(str,"abcdefg"); const char * str1 = str; char * const str2 = str; //str1[1] = 'c'; //str2 = str1; return 0; } |
Line 9 means, str1 is a pointer to constant character string which means that once you assigned it a string, You can't change content of the string with pointer str1.
i.e. if I uncomment line #11, it will give us compilation error.
Line 10 means, str2 is constant pointer to character string which means that pointer is constant not string i.e. You can not make str2 to point to some another pointer.
i.e. if I uncomment line #12, it will give us compilation error.
But following lines are valid.
1 2 | str1 = str2; str2[0] = 'c'; |
One thumb of rule that will help you to remember is that you start reading from right to left. e.g.
If you read line #9, you should read it as str1 is pointer to character string that is constant
If you read line #10, you should read it as str2 is constant pointer which points to character.
Now isn't it easy to understand?
Now let us see what does following code mean?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | int main() { char* str = new char[8]; strcpy(str,"abcdefg"); const char * const str3 = str; //str3 = str2; //str3[3] = 'a'; char const *str4 = str1; // equivalent to const char *str4 //str4[0] = 'c'; char const * const str5 = str; // eqivalent to const char * const str5 return 0; } |
If I uncomment line #6 and #7 it will give us compilation error because neither you can't change content of string nor content of pointer i.e. pointer can't point to a different string.
Line #9 means that str4 is a pointer that points to constant character string. Hence str4 and str1 in first code snippet are equivalent. There are two ways to write same statement.
Similarly str5 and str3 are equivalent.
In nut shell, if you remember the thumb rule(i.e. read from right to left) You don't have to memorize anything.
No comments:
Post a Comment