简体   繁体   中英

invalid conversion from ‘const char*’ to ‘char’

I am trying to replace a certain character in a string with a space using the following code line:

str[i] = " ";

How can realize this without getting the error in the title of the question?

use single quotes

str[ i ] = ' ';

In C++, the token " " is a string literal which represents an array of two characters: the value of a space in the character set (eg, the value 32 in ascii) and a zero. On the other hand, the token ' ' represents a single character with the value of a space (usually 32). Note that in C, the token ' ' represents an integer with the value of a space. (In C, sizeof( ' ' ) == sizeof( int ), while in C++, sizeof( ' ' ) == 1.)

Single char literals are obtained with single quotes:

str[i] = ' ';

A literal with double-quotes is a full string literal (a null-terminated array of char ), but you're only replacing a single char .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM