【发布时间】:2015-06-20 12:21:24
【问题描述】:
鉴于此代码:
void group::build(int size, std::string *ips){
/*Build the LL after receiving the
member list from bootstrap*/
head = new member("head");
member *temp1,*temp2;
temp1 = new member(ips[0].data()); // error here
// ....
}
member::member(char *ip){
strcpy_s(this->ip_addr,sizeof(ip),ip);
this->next = NULL;
this->previous = NULL;
}
而指向字符串的指针定义为:
std::string *ips;
我想用字符串初始化数组ips,但是当我尝试从任何数组成员获取 char 数据时出现错误:
无法将参数从
const char *转换为char *
为什么?
【问题讨论】:
-
strcpy_s的使用是错误的。首先,第二个参数是目标缓冲区的大小,但您没有提供。其次,sizeof(ip)是char*的字节大小,与字符串长度无关。