How does printf display a string from char *?

Let's look at a simple example.

Eg: const char* letter = "hi how r u";

letter is a pointer to a const character that points to the string "hello, like r u." Now that I want to print data or access data, should I use it *lettercorrectly?

But in this situation, I do not need to use only the address in the printf call?

printf("%s",letter);

So why is that?

+3
source share
6 answers

*letter- actually a symbol; this is the first character that it points to letter. If you work with the entire string of characters, then by convention, the functions will look at that character, and the next one, etc., until you see a null byte ('\ 0').

, (.. ), , - , , , . char* ; .

+7

, printf ​​: int printf(const char* format, ...);, , () char, .

+1

, , , char.

( *), .

prinf ( ), , , '\ 0'.

++, , encapulated std::string, iostreams, :

std::string line="hi how r u";
std::cout << line << std::endl;
+1

printf("%s") , NULL (\0) = . letter. printf("%c") : printf("%c", *letter);

+1

printf . , ( ) %s %d, %e, %f .., *. , printf (char - 1 2 , int - 4, ..), EOL .

Of course, if you pointed to an array variable, then you need to dereference this pointer with *. But this is more an exception than a rule. :)

+1
source

All Articles