A char pointer is the address of the memory location where your string begins. A char is just a single character. A valid piece of code would be char qu[] = {'N', 'G'};.
It says that even though the type of "hello" is constchar* (that is, the contents of "hello" cannot be modified), the line char* ptr = "hello"; will compile fine. Although it's not a good thing to do and the same wouldn't work with const and non-const pointers in any other case. Reasons why someone thought this was a good idea are unknown to me. There is nothing to gain here. Unless you hate typing const.
It is undefined behaviour to modify a string literal, and is most likely the cause of the crash in your program (ISO C++: 2.13.4/2). The standard allows for a conversion from a string literal to char* for backwards compatibility to C and you should only have that conversion in your code if you absolutely need it.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
usingnamespace std;
int main()
{
cout << boolalpha;
char* evil = "evil thing to do"; // literal is in const memory
// Verify that the calling process has read access to the specified range of memory. [3]
bool bCanRead = (FALSE == IsBadReadPtr(evil, sizeof(evil)));
// Verify that the calling process has write access to the specified range of memory. [3]
bool bCanWrite = (FALSE == IsBadWritePtr(evil, sizeof(evil)));
cout << "Can read = " << bCanRead << endl;
cout << "Can write = " << bCanWrite << endl;
return 0;
}
Can read = true
Can write = false
Andy
[3] But you shouldn't use IsBadReadPtr/IsBadWritePtr is real code. From MSDN
Important This function is obsolete and should not be used. Despite its name, it does not guarantee that the pointer is valid or that the memory pointed to is safe to use. For more information, see Remarks on this page.
But it is safe to use in this tiny example! (These functions try to read from/write to the memory location specified, and hande any exceptions that are raised; Windows structured exceptions rather than C++ ones.)