Mick Sharpe said:
ptr[0] is the first char of the string and &ptr[0] is its address, which is
what %s is expecting. Try:
printf("%lx\n", (long)&ptr);
instead
No, don't do that. The "%lx" format expects an unsigned long
argument; you're passing a (signed) long. But that's probably not
going to cause any problems. The real problem is that you're
converting a pointer value to type long; this might or might not give
you a meaningful result.
The correct way to print a pointer is to use the "%p" format, which
expects a void*:
printf("%p\n", (void*)ptr);
The cast to void* is necessary because ptr is of type char*, and
printf with a "%p" format expects a void*. (Actually, it's not
strictly required in this case, because the language specifies that
void* and char* have the same representation, but that's a special
case and you're probably better off ignoring it.)
The above will print (some system-specific textual representation of)
the address of the memory location to which ptr points, which happens
to be the beginning of the string "test me".
Note that
printf("%p\n", (void*)&ptr);
is also legal, but does something different; it prints the address of
"ptr" itself.