从“const char *”到“char”的无效转换 [英] invalid conversion from ‘const char*’ to ‘char’
问题描述
我尝试使用以下代码行将一个字符串中的某个字符替换为空格:
str [i ] =;
如何实现这一点而不会在问题的标题中出现错误?
$ b
str [i] =' ';
在C ++中,标记是一个字符串文字,代表两个字符的数组:字符集中的空格的值(例如,ascii中的值32)和零。另一方面,令牌'表示具有空格值(通常为32)的单个字符。请注意,在C中,令牌'表示具有空格值的整数。 (in C,sizeof('')== sizeof(int),而在C ++中,sizeof('')== 1。)
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.)
这篇关于从“const char *”到“char”的无效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!