c++ - Whats the error in it. string assignment -
i take data struct elements internal elements. better way it. shows error: invalid array assignmen berror: invalid array ssignment error: invalid array assignment error: ‘strcpy’ not declared in scope.
#include <iostream> #include <string> using namespace std; struct { 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.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; }
i think mean following (c string functions declared in header <cstring>
)
#include <cstring> //... char m_ip[16]; char m_port[6]; char m_sessionkey[32]; 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);
or instead of
a = { "10.43.160.94111", "12345", "12abcd12345abcd12345abcd1234512" };
you write
a; = { "10.43.160.94111", "12345", "12abcd12345abcd12345abcd1234512" };
provided compiler supports c++ 2011.
take account forgot place semicol after closing brace in structure definition
struct { //... }; ^^^
edit: after unexpectedly changed code i'd point out code snippet
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);
does not make sense. there no sense introduce objects of type std::string
initialize object of type struct a
.
another thing define structure following way
struct { std::string ip; std::string port; std::string sessionkey; };
Comments
Post a Comment