   Pointer to a Char String Question
  I've been studying pointers the past few days and can't wrap my head around the relationship between pointers and a char array.From what I understood a pointer will just point to the Memory address of variable or memory allocated from the heap.So how is it possible for a pointer to point directly to a string of characters???In my readings it has been explained that you can't point directly at an integer which made sense. (you can't just point to the number 1 as it's just a number and has no address in memory).Take the following example:char *pChar = "GameDEV";//Works Correctlyint *pInt = 12;//Doesn't WorkWhy would c++ allow this to point to a string and not a integer?What would be the point of pointing directly to a string like this??Could you even change the Value of it?Most Importantly how does it do it? How Does it point directly to a string which isn't assigned a memory location?? I know this is where everyone tells me i'm an Idiot but I wanna know how this works as it keeps bothering me in the back of my mind.Many ThanksDavid   
  because "GameDEV" is an array of characters while 12 is just a single integer;  The character array "GameDev" is already allocated inside the program.  By char *pChar = "GameDev", you assign the address of the already-allocated memory to pChar.The statement char *pChar = "GameDev"; literally reads "assign the address of the character array to pChar"The statement int *pInt = 12; literally reads "assign the 12th byte in memory to pInt"If you were to allocate memory for pInt and assign that new integer a value in one sweep:  int *pInt = new int( 12 );  (don't forget to delete it!)Also, arrays (and C-style strings) are nothing more than pointers to the first element in the array.  In the context of C-style strings, the end of the array is determined by the null character '\0' at the end of the string.  With arrays, you must store the number of elements of the array separately, or you can use a value that denotes the end of the array.   ;Quote:Original post by _fastcallAlso, arrays (and C-style strings) are nothing more than pointers to the first element in the array.  In the context of C-style strings, the end of the array is determined by the null character '\0' at the end of the string.  With arrays, you must store the number of elements of the array separately, or you can use a value that denotes the end of the array.Not quite. Arrays and pointers are distinct. Arrays decay to pointers in certain expressions.You are quite incorrect about needing to store the length of an array separately. The length of an array is part of its type - you can take advantage of this fact to write a generic function to return the length of an array:template<class T, int N>int array_size(const T (&) [N]){    return N;}Note that the array is passed by reference because arrays cannot be passed by value to functions - which is often the source of the confusion about arrays and pointers because the size information is lost when the array that is passed as an argument!There are other ways in which pointers and arrays differ aswell. You can assign to a pointer, but not an array. The sizeof(pointer) is different from the size of an array. The address of a pointer is distinct, but the address of an array is the same as the address of the first element.   ;  You might want to try reading through this first.Quote:Original post by thekiwimaddogI've been studying pointers the past few days and can't wrap my head around the relationship between pointers and a char array.From what I understood a pointer will just point to the Memory address of variable or memory allocated from the heap.So how is it possible for a pointer to point directly to a string of characters???It points at the beginning of the string.Quote:Take the following example:char *pChar = "GameDEV";//Works Correctlyint *pInt = 12;//Doesn't WorkWhy would c++ allow this to point to a string and not a integer?The first example isn't "pointing to a string"; it's taking an array of characters, implicitly converting the array to a pointer, and assigning that pointer value to a variable that stores a pointer.The second example isn't attempting to "point to an integer"; it's attempting assign the integer to the pointer. If you wanted to get a pointer to the number "12", you would have to use the address-of operator. But the value '12' does not have an address, so this still doesn't work.This is nothing to do with any difference between chars and ints, and everything to do with the fact that "GameDEV" is an array of chars. Arrays are treated differently from single values in that they are pre-allocated and written into the program binary. (This may be a simplification, but it's what happens practically speaking. I don't know exactly what the Standard has to say about it.)Note that you can also declare pChar to be of type "array of char" instead of type "pointer to char"; this has the effect of copying the array (in much the same way that 'int a = 12;' loads the value 12 into a, but has no effect on anything else in your program that happens to be equal to 12).You can also point to an array of integers: "int* pInt = {1, 2, 3};". But you can't take the address of an integer literal, nor of a character literal.Quote:What would be the point of pointing directly to a string like this??Could you even change the Value of it?The syntax of the language allows you to, but it's actually not legal per the language standard. The reason, basically, is that the operating system might not be too happy about you trying to write to the block of memory that the string resides in (because it's part of the program itself). Just one of those accidents in the history of the language design.However, if you explicitly copy the array into a char[] variable, you can certainly modify the copy.   ;Quote:Original post by ZahlmanYou can also point to an array of integers: "int* pInt = {1, 2, 3};".I'm pretty sure that's not possible.Quote:Original post by ZahlmanJust one of those accidents in the history of the language design.Strictly speaking, it was not an accident, but a conscious decision for backward compatibility reasons. There was simply too much char *p = "hello"; code out there that would have been rendered illegal by changing the type of string literals (C) or the implicit conversion rules (C++).   ;Quote:Original post by DevFredQuote:Original post by ZahlmanYou can also point to an array of integers: "int* pInt = {1, 2, 3};".I'm pretty sure that's not possible.Zahlman was near.int main () {   int i[] = {1,2,3};   char c[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0' };   char c2[] = "Hello World";}String literals are special in that their address can directly be written into a pointer to char. One more special thing is that the compiler adds an \0 at the end.   ;  Erm, right; there are no array literals (in C++, yet), so of course you can't point at one; you can only use that syntax to initialize an actual array. And strings are in fact a special case. Sigh. Thanks, C. ^^;   ;  Moved to For Beginners.   ;Quote:Original post by Zahlmanthere are no array literals (in C++, yet)And it won't get them soon. The closest thing to an array literal is an std::initializer_list. C99 has array literals btw.   