Interview Question
There is no difference. Both are pointing to constant strings. Both are free to point to any other string of any length ... Of course, in the first one you have already declared that ch is an array of character types, whose length is fixed whereas in the second one, one can reassign it a character array of any length
incorrect. first one cannot point to any other string. it has memory reserved. one can only change the contents of that string which can be done by changing the elements of that array.
the second one can point to any other string, as its just a non-constant pointer.
first one is actually a constant pointer to the memory area reserved by the array. and the array is not constant
i agree with AK
in my opinion first one is like saying char * const ch="Ab";
And csk too that u cant expand the memory pointed by it now.
Given the following:
char A[] = "ab";
char* B = "cd;
A is of type char[3] and B is of type char*. They are not equivalent types, but they are very similar and most compilers allow you to interchange them in many contexts, but not all. For example;
B = A; // Implicit conversion of char[3] to char*, OK
strlen(A) // Same implicit conversion
A = B; // ERROR cannot convert from
// 'char*' to 'char[3]'
Reference "The C++ Progamming Language, Special Edition By Bjarne Stroustrup" pp 91-92 section 5.3
first one is constant pointer to non-constant data
- Anonymous June 08, 2010seccond one is non-constant pointer to constant data