在C ++(编译器:clang ++)中,编译以下代码时:
char* strcpy(char * dest, const char * src)
{
char* result = dest;
if(('\0' != dest) && ('\0' != src))
{
/* Start copy src to dest */
while ('\0' != *src)
{
*dest++ = *src++;
}
/* put '\0' termination */
*dest = '\0';
}
return result;
}
我收到以下错误代码:
string/strcpy.cpp:12:11: error: comparison between pointer and integer
('int' and 'char *')
if(('\0' != dest) && ('\0' != src))
~~~~ ^ ~~~~
string/strcpy.cpp:12:29: error: comparison between pointer and integer ('int'
and 'const char *')
if(('\0' != dest) && ('\0' != src))
我知道当要比较的字符在引号而不是撇号之间时会产生与此错误相关的大多数错误,但在此代码中并非如此。为什么会出现此错误?提前谢谢!
答案 0 :(得分:1)
('\0' != dest)
在上文中,'\0'
是字面值,但dest
是字符的指针。这就是抛出错误的原因。
我认为如果将条件更改为
,问题将得到解决('\0' != *dest)
编辑:哎呀,我以为你试图检查字符串是否已经结束了。如果您试图检查指针是否为空,那么正确的方法是将其评估为条件。
if(dest)
答案 1 :(得分:1)
class App extends Component {
constructor(props) {
super(props);
this.state = {
moduleList: [{ id: UniqId(), removeModule: false }],
};
this.onAddChild = this.onAddChild.bind(this);
this.className = bemClassName.bind(null, this.constructor.name);
}
onAddChild(module) {
const moduleList = this.state.moduleList;
this.setState({
moduleList: moduleList.concat({
id: UniqId(),
removeModule: false,
}),
});
}
onRemoveModule = ( i, arr ) => (e) => {
const moduleList = this.state.moduleList;
e.preventDefault();
moduleList[i].removeModule = true;
this.setState({ moduleList: moduleList });
}
render() {
const { className } = this;
return (
<div className={className('container')}>
<Header onAddChild={this.onAddChild} />
<div className="cf">
{this.state.moduleList.map(
( delta, index ) => {
if ( !this.state.moduleList[index].removeModule ) {
return (
<ColorModule
className="cf"
onRemove={this.onRemoveModule( index, this.state.moduleList )}
index={index}
key={delta.id}
moduleId={'colorModule' + delta}
/>
);
}
}
)}
</div>
</div>
);
}
}
甚至更惯用
if((NULL != dest) && (NULL != src))