I'm confused! Char, Char * or String in C ++

when I use char or char *, visual studio 2012 (11) is just the last couts character, for example:

#include <iostream> #include <string> int main(){ using namespace std; char chName = 'Alex'; cout<<chName; } 

Only "x" is displayed. this is correct, I use string strName = "Alex" but in that function that has a parameter like char, the string cannot be passed as an argument. in this case, the VS compiler says that strings cannot be converted to int. also tell me what is the difference between char and char *. I am a PHP developer, and C ++ is so confusing. Please help me.

+4
source share
4 answers
 char chName = 'Alex'; 

is a multi-channel literal, and it is determined by implementation.

 _ 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. 

you should use instead

 const char *chPTR = "Alex"; 

or

 char chName[] = "Alex"; 

The difference between char and char *
In char ch; ch is a char variable that can hold a single ascii character, whereas char *ch; is a pointer to a char that can hold the address of a char variable.

The difference between char and string
see this SO post .

+2
source

char can only contain 1 character at a time; in this case, it saves your last character, 'x' . char * is a pointer to one or more char objects, and when read correctly, it can also be used as a string. Therefore installation

 const char *chName = "Alex"; cout << chName; 

should print the whole name.

Another problem is your use of quotes. 'x' stands for a char , and "x" stands for the char s array, known as a string literal.

If there is a function that requires passing char *, you can pass

 const char *param = "this is da parameter"; function (param); 

or

 std::string param = "da parameter"; //std::string is a type of string as well. function (param.c_str ()); 

You can also use an ad.

 char chName[] = "Alex"; 

This would create a local char array (namely, 5 char s, because it adds a null character at the end of the array). Therefore, the call to chName[3] should return 'x' . This can also be passed to cout , like the rest

 cout << chName; 

EDIT: By the way, you should return int in your main () function. Like 0 .

+7
source
 char chName = 'Alex'; 

It is not right. Thus, you create a four-byte integer from Alex, then you store it in char - of course, it does not fit into a single-byte char, therefore only its less significant byte, x stored, and then output. You have to use

 const char *chName = "Alex"; 

to get the right conclusion.

Alternative to your problem: use std::string , since you are working in C ++, but for those functions that expect char * as their argument, use:

 std::string str; // C++ string object function_call(str.c_str()); 
+4
source

The char type contains a single integer value, usually with a range from -128 to 127. This is not a string type. Single quotes in C are used for character literals, not string literals. 'Alex' does not match "Alex" .

The literal char syntax you used is:

 char chName = 'Alex'; 

is called a multi-character literal, and it has a specific implementation value of type int . Implementations I am familiar with the construction of this by concatenating the values ​​of individual characters. So the value of 'Alex' is probably A 0x41, l 0x6C, e 0x65, x 0x78 or 0x416C6578 . Then, when you assign it to char, it is truncated only to the last byte (since it can contain all char), i.e. 0x78, or the same as 'x' .

* is the pointer operator of a pointer in C, and it is also used when declaring a pointer. Therefore const char *chName = "Alex"; declares a char pointer, not a single char , and that pointer points to the first character of the string literal "Alex" . So your program might look like this:

 #include <iostream> int main(){ const char *chName = "Alex"; std::cout << chName; } 
+1
source

All Articles