这里面有什么错误?字符串赋值
Whats the error in it. string assignment
我想把数据从结构元素到内部元素。做这件事有什么更好的方法?它显示错误:无效数组分配错误:无效数组分配
没有在这个范围内声明' strcpy '。#include <iostream>
#include <string>
using namespace std;
struct A
{
char Ip[16];
char port[6];
char sessionkey[32];
}
int main()
{
char m_ip[16];
char m_port[6];
char m_sessionkey[32];
A a;
a.Ip = "10.43.160.94111";
a.port = "12345";
a.sessionkey = "12Abcd12345Abcd12345Abcd1234512";
strcpy(m_ip,a.Ip);
strcpy(m_port,a.port);
strcpy(m_sessionkey,a.sessionkey);
cout << "m_ip:" << m_ip << endl;
cout << "m_port:" << m_port << endl;
cout << "m_sessionkey:" << m_sessionkey << endl;
}
我想你的意思是以下(C字符串函数在标题<cstring>
中声明)
#include <cstring>
//...
char m_ip[16];
char m_port[6];
char m_sessionkey[32];
A a = { "10.43.160.94111", "12345", "12Abcd12345Abcd12345Abcd1234512" };
std::strcpy(m_ip,a.Ip);
std::strcpy(m_port,a.port);
std::strcpy(m_sessionkey,a.sessionkey);
或者代替
A a = { "10.43.160.94111", "12345", "12Abcd12345Abcd12345Abcd1234512" };
你可以写
A a;
a = { "10.43.160.94111", "12345", "12Abcd12345Abcd12345Abcd1234512" };
如果你的编译器支持c++ 2011。
请考虑在结构定义
中忘记在右括号后放置分号struct A
{
//...
};
^^^
EDIT:在您意外更改代码之后,我想指出这段代码片段
A a;
string p = "10.43.160.94111";
string q = "12345";
string r = "12Abcd12345Abcd12345Abcd1234512";
p.copy(a.Ip,16,0);
q.copy(a.port,6,0);
r.copy(a.sessionkey,32,0);
没有意义。没有必要引入std::string
类型的对象,只有初始化struct A
类型的对象。
另一件事你可以这样定义结构
struct A
{
std::string Ip;
std::string port;
std::string sessionkey;
};
在c++中更喜欢使用std::string
而不是char *
或char[]
。如果您使用std::string
,那么您的许多问题将不再存在。
的例子:
#include <iostream>
#include <string>
struct A
{
std::string Ip;
std::string port;
std::string sessionkey;
};
int main()
{
std::string m_ip;
std::string m_port;
std::string m_sessionkey;
A a;
a.Ip = "10.43.160.94111";
a.port = "12345";
a.sessionkey = "12Abcd12345Abcd12345Abcd1234512";
// copy data from a to local variables
m_ip = a.Ip;
m_port = a.port;
m_sessionkey = a.sessionkey;
std::cout << "m_ip:" << m_ip << std::endl;
std::cout << "m_port:" << m_port << std::endl;
std::cout << "m_sessionkey:" << m_sessionkey << std::endl;
}
如果你坚持使用strcpy
,你必须通过使用#include <string.h>
或#include <cstring>
来包含C头文件string.h。请注意,这是一个C头文件,它与c++ #include <string>
头文件明显不同。
你应该这样修改你的代码:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct A
{
char* Ip;
char* port;
char* sessionkey;
};
int main()
{
char m_ip[16];
char m_port[6];
char m_sessionkey[32];
A a;
a.Ip = "10.43.160.94111";
a.port = "12345";
a.sessionkey = "12Abcd12345Abcd12345Abcd1234512";
strcpy(m_ip,a.Ip);
strcpy(m_port,a.port);
strcpy(m_sessionkey,a.sessionkey);
cout << "m_ip:" << m_ip << endl;
cout << "m_port:" << m_port << endl;
cout << "m_sessionkey:" << m_sessionkey << endl;
}
strcpy()
函数在c++/c++ 11的cstring
头文件中,所以你必须在你的代码中添加#include<cstring>
。
相关文章:
- 如何使用向量为字符串赋值
- 避免在 ctor 初始值设定项列表中的字符串赋值中使用双重调用函数
- 无法为字符串赋值(Eclipse CDT、MinGW、C++)
- 带有初始值设定项列表的字符串赋值
- R 值重载运算符字符串赋值错误
- 使用for循环的C字符串赋值
- 正在将std::string中的子字符串赋值为c样式字符串
- 字符串赋值中的代码转储
- 当我们可以使用=运算符为字符串赋值时,为什么要在赋值之前使用new运算符为字符串动态分配内存
- 析构函数忽略字符串赋值
- 如果右手边包含字符串文字的串联,则C++const std::字符串赋值错误
- 在c++中如何从另一个字符串赋值未初始化的字符串值
- 字符串赋值错误
- 蠢Q:将字符串赋值到字符数组中
- 如何将字符串赋值为十六进制
- 字符串赋值差异
- std::通过索引进行字符串赋值似乎不起作用
- 当常量字符串赋值给常量字符指针时会发生什么?
- 字符串赋值中的Coredump
- 这里面有什么错误?字符串赋值