Magicaxis
Magicaxis

Reputation: 381

Why is my single char more than one character long?

I'm trying to create a char* made of the first character of a string. The string kept inside a vector of strings.

I have tried the following:

char* value = &cards[0][0]; cout<<"Value "<<i<<" == "<<value<<endl;

char* value = &cards[0].front(); cout<<"Value "<<i<<" == "<<value<<endl;

('cards' is a vector of strings, each string is a card in a regular 52 card deck)

but whatever I do, value is always equal to the entire string.

How do I make value a single letter?

Upvotes: 0

Views: 206

Answers (1)

Michael Liu
Michael Liu

Reputation: 55359

By convention, a char* is interpreted as a null-terminated string literal. So cout outputs everything from the character that value points to up through the end of the string.

If you just want a single character, you can use char instead of char*:

char value = cards[0][0];
cout << "Value " << i << " == " << value << endl;

Upvotes: 6

Related Questions