Why does this for loop work? (C++ pointers)
Please consider the following: [code] char name[]{ "Mollie" }; int arrayLength{ static_cast<int>(std::size(name)) }; //7 for (char* ptr{ name }; ptr < name+arrayLength; ++ptr){} [/code] The for loop assigns a pointer that points to the first element of name. The pointer is then incremented up to the null terminator of the name. What I cannot understand is how the compiler is interpreting ptr < name+arrayLength as: 'Continue this for loop for as long as the current pointer address is less than the starting memory address + the length in bytes of 7 x chars; essentially the address of the null terminator of name.' How is the compiler implicitly converting 'name+arrayLength' (an array of chars + an int) to memory addresses? I can understand how it would work if I wrote something like "&name[0] + (arrayLength*size.of(char)". However, this would fail unless I had previously declared a pointer to hold this value. The value, say char* endPointer, could then be used for the for loop comparison. What is going on here? Thanks!