I'm Confused! Char, Char* or String in C++
c++, c-strings
Solution
char chName = 'Alex';
is a multicharacter literal and it is implementation defined.
_ C++ standard, §2.14.3/1 - Character literals _
An ordinary character literal that contains more than one c-char is a multicharacter
literal . A multicharacter literal has type int and implementation-defined value.
instead of this, you should use
const char *chPTR = "Alex";
or
char chName[] = "Alex";
Difference between char and char* In `char ch;` ch is a char variable which can store a single ascii character, whereas `char *ch;` is a pointer to char which can store address of a char variable.
Difference between char and String see this SO post.
Problem
when i use char or char*, visual studio 2012(11) only couts last character such as: ``` #include <iostream> #include <string> int main(){ using namespace std; char chName = 'Alex'; cout<<chName; } ``` It displays only "x". it is correct is i use `string strName = "Alex"` but in those function which have parameter as a char, string can not be passed on as argument. in this case, VS compiler says that strings cant be converted into int. also tell me what is difference between char and char*. I am a PHP developer and C++ is so confusing. Please help me.