这里面有什么错误?字符串赋值

Whats the error in it. string assignment

本文关键字:字符串 赋值 错误 什么 这里      更新时间:2023-10-16

我想把数据从结构元素到内部元素。做这件事有什么更好的方法?它显示错误:无效数组分配错误:无效数组分配

没有在这个范围内声明' 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>