0
#include <stdio.h>

char s[]="sfksfls\n";
void main()
{
        printf(s);

}

Why it can work? I just input the char pointer to printf

James
  • 17
  • 1
  • 1
    Doing that is a really bad idea, btw. What if `s` points to a string like `"foo %d bar"`? Use `fputs(s, stdout);` to print just a string. – Shawn Oct 31 '18 at 03:07
  • 1
    It can work. You can even add a *shift-state* as an integer before `s` to tell `printif` which character to begin output with. But it isn't wise to do ti that way, and it makes code much more difficult to read and maintain. Since you have a string in an array, simply us the `"%s"` *format conversion-specifier* to output the contents of `s`. Doing so provides access to all modifiers available, (e.g. *field-width* modifier, padding and preservation of space for signed values, etc..) – David C. Rankin Oct 31 '18 at 07:01

1 Answers1

1

A string literal in C is a pointer to const char. printf takes a pointer to const char (plus an optional list of additional arguments). Passing it directly or through a variable makes no difference, a pointer is a pointer, it's just a number.

Cyber
  • 857
  • 5
  • 10
  • 1
    Except here we do not have a pointer to a *string literal*, we have an array that was initialized using a *string literal*, but for all relevant purpose here is just an array containing a string. – David C. Rankin Oct 31 '18 at 07:02