Gustavo Lopes wrote:
const char * and char const * are the same (just like const int and int const are the same); what's not the same is char *const.

Andrey Hristov wrote:
it's easy, whatever const is closer to is immutable
const char * is a pointer to a const char, because the const is closer to the char than to the pointer. char * const is a const pointer to a char, because the const is closer to the pointer than to the char. Anyway, never used char const *, which will conflict with my explanation :)

Best,
Andrey


I stand corrected, char const * is the same as const char* [1], so any of them would
work for the ini struct.
The C++ FAQ Lite recommends reading it right to left [2], but then the leftmost const case is left undefined by the mnemonic, so it isn't valid for all cases, either.

I'm also attaching a small program showing the different ways of accessing each of those
and the expected warnings so that your favorite compiler can yell at you :)


[1] http://www.parashift.com/c++-faq/const-correctness.html#faq-18.9
[2] http://www.parashift.com/c++-faq/const-correctness.html#faq-18.5


int main() {
	const char* char_const1 = "ABC";
	char_const1[0] = 'B'; /* Error */
	char_const1 = "DEF"; /* Ok */
	
	char const* char_const2 = "ABC";
	char_const2[0] = 'B'; /* Error */
	char_const2 = "DEF"; /* Ok */
	
	char * const ptr_const = "ABC";
	ptr_const[0] = 'B'; /* Ok */
	ptr_const = "DEF"; /* Error */
	
	char const * const const_const = "ABC";
	const_const[0] = 'B'; /* Error */
	const_const = "DEF"; /* Error */
	
	return 0;
}

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to