I am thinking of something like:
#include
#include
#include
int main(void) {
//test pointer to string
c
The "%s"
format specifier for printf
always expects a char*
argument.
Given:
char s[] = "hello";
char *p = "world";
printf("%s, %s\n", s, p);
it looks like you're passing an array for the first %s
and a pointer for the second, but in fact you're (correctly) passing pointers for both.
In C, any expression of array type is implicitly converted to a pointer to the array's first element unless it's in one of the following three contexts:
(I think C++ has one or two other exceptions.)
The implementation of printf()
sees the "%s"
, assumes that the corresponding argument is a pointer to char, and uses that pointer to traverse the string and print it.
Section 6 of the comp.lang.c FAQ has an excellent discussion of this.