6

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.

1
  • 4
    When using C++ use string. Inside double quotes is a constant string.If you want to return a char* from a string use c_str(). Check the link stackoverflow.com/questions/388242/… Commented Jun 19, 2012 at 16:48

4 Answers 4

8

char can only keep 1 character at a time; in this case, it keeps your last character, 'x'. char * is a pointer to one or more char objects, and if read correctly, can also be used as a string. So setting

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

should output the whole name.

Another problem is your use of quotations. 'x' denotes a char, while "x" denotes a array of chars, known as a string literal.

If there is a function that requires you to pass a char *, you could 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 could also use the declaration

char chName[] = "Alex"; 

This would create an local array of chars (namely, 5 chars, because it appends a null character at the end of the array). So, calling chName[3] should return 'x'. This can also be streamed to cout like the others

cout << chName; 

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

Sign up to request clarification or add additional context in comments.

5 Comments

I don't know about "you should return an int in your function main." C++ allows main (but no other function) to omit the return statement. If it is omitted, 0 is returned.
@Robᵩ: C++ allows a lot of silly things. You should return an int from main even if you don't have to.
@MooingDuck, what for, if return 0 is implicit?
@MooingDuck, both of them are nonsense in this case.
" If there is a function that requires you to pass a char *, you could pass " No nonono, only if it's one that wants a const char*.
4
char chName = 'Alex'; 

This is wrong. This way, you create a four-byte integer out of Alex, then you store it in a char -- of course, it doesn't fit into a one-byte char, so only its less significant byte, the x gets stored, which is then output. You need to use

const char *chName = "Alex"; 

to get the correct output.

An alternative to your problem: use std::string, as you're working in C++, but for those functions who expect a char * as their argument, use:

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

7 Comments

Better to use a proper C++ string though, rather than an old skool C-style char *.
I'm not saying which is better; I'm answering OP's question. (Btw C being "old skool" is not necessarily true. Try to write an operating system in C...)
@H2CO3: Writing an operating system is a completely niche use and does not whatsoever prevent C from being a worthless pile of junk in the general case.
@H2CO3: We're talking about C++, not C. char* is definitely outmoded for most purposes in C++.
"prevent C from being a worthless pile of junk in the general case" Have you ever written a single line of C?
|
3

The char type holds a single integral value, usually with the range -128 to 127. It is not a string type. Single quotes in C are used for character literals, not string literals. 'Alex' is not the same as "Alex".

The char literal syntax you used:

char chName = 'Alex'; 

is called a multi-character literal, and it has an implementation defined value of type int. Implementations I'm familiar with construct this by concatenating the values of the 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 a char it gets truncated to just the last byte (since thats all a char can hold), which is 0x78, or the same as 'x'.

The * is the pointer deference operator in C, and it's also used when declaring a pointer. So const char *chName = "Alex"; declares a pointer to a char rather than a single char, and that pointer will point to the first character of the string literal "Alex". So your program might look like:

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.