c++ - const char 转 char 问题

标签 c++

如何将 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/

相关文章:

c++ - 在 Visual Studio 2010 中使用 boost::test - 如何查看测试输出?

Windows 上的 C++ 在启动后立即关闭程序

c++ - 如何知道何时有最大数量的命名管道客户端连接到服务?

c++ - 抽象具有不同返回类型的成员函数

c++ - 加快数据记录代码

c++ - 使用flex的Makefile错误

c++ - 如何忽略输入中的n个整数

c++ - 如何让 MPI_Send 让处理器按顺序发送而不是随机发送?

c++ - C++中的文件输入/输出

c++ - 是否有与 Linux 的 MulDiv 等效的功能?