1
1
123qwe2015-10-29 19:18:38
C++ / C#
123qwe, 2015-10-29 19:18:38

Why can a string be passed to a char pointer?

What's up, software.
I'm trying to figure it out with memory, and one more issue.

void print(char *b)
{
  cout << b << endl;
}

For some reason, you can pass here what will be in double quotes, like "fjkdfjlkdfjlkdfj". I don’t quite understand, this is a pointer to char in the argument, and therefore you need to pass the address there. What am I wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jcmvbkbc, 2015-10-29
@Yonghwa

"fjkdfjlkdfjlkdfj" is a string literal. In most contexts, it is interpreted as a char array.
An array, when passed to a function, is converted to a pointer to its first element.

M
MiiNiPaa, 2015-10-29
@MiiNiPaa

A string literal is of type const char[x], where x is the size of the array needed to hold the string.
Such an array can be implicitly converted to const char*.
Some compilers that do not respect the standard allow you to drop const from string literals.

O
Oleg Tsilyurik, 2015-10-29
@Olej

you can pass string

... but be careful with the word string in C++ - in C++ it means a completely concrete type (coming from the STL) ... and not just anything ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question