如何将 char
转换为 const char
?如何在不更改 char SendBuf
的情况下实现类型转换?这是我的代码:
.cpp
// main()
char SendBuf[6] = "Hello";
sendto(..., SendBuf, ...);
// sendto structure
int sendto(
__in SOCKET s,
__in const char *buf,
__in int len,
__in int flags,
__in const struct sockaddr *to,
__in int tolen
);
错误
Error 1 error C2440: '=' : cannot convert from 'const char [6]' to 'char [6]'
谢谢。
最佳答案
我手边没有微软编译器来测试这个理论,但我怀疑 OP 有这样的代码:
int main() {
char SendBuf[6];
SendBuf = "Hello";
// sendto(..., SendBuf, ...);
}
我怀疑他看到的错误 cannot convert from 'const char [6]' to 'char [6]'
发生在赋值上,而不是初始化也不是 sendto
调用。
有微软编译器的人可以检查编译上面的程序并确认错误信息吗?
关于c++ - const char 转 char 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5356072/