我无法打印字符数组的值,即使我使用第一个元素的地址传递数组:
vbv_packet = Packet::create(&username[0],
&password[0], Packet::VBV, Packet::DATA,
bytea_from_memptr(& new_mr, car_multirecord::HEADER_SIZE +
(new_mr.get_count() * sizeof(car_compact_record))));
在定义函数中,我尝试打印像这样的数组值:
static Packet::ptr create(const char* username, const char* password,RTCINET_COMMANDS cmd, RTCINET_COMMANDS subcmd, Packet::ptr_bytea ba)
// TODO: unsigned short command_seq, data_seq, length
{
//Add user authentication fields, username n password here
std::cout << "********** CREATING PACKET";
std::cout << "sizeof username: " << strlen(username);
std::cout << "sizeof password: " << strlen(password);
for(int i = 0; i < strlen(username); i++)
{
std::cout << *(username + sizeof(char) * i);
}
std::cout << "\n";
for(int j = 0; j < strlen(password); j++)
{
std::cout << password[j];
}
不用说,这两个字符串定义为:
const char username[] = "ARG-CO";
const char password[] = "xxxxx!";
我在哪里做错了?
答案 0 :(得分:0)
vbv_packet = Packet::create(username,
password, Packet::VBV, Packet::DATA,
bytea_from_memptr(& new_mr, car_multirecord::HEADER_SIZE +
(new_mr.get_count() * sizeof(car_compact_record))));
应该是正确的,你想在这里实现什么:
for(int i = 0; i < strlen(username); i++)
{
std::cout << *(username + sizeof(char) * i);
}
你为什么这样做*(username + sizeof(char) * i)
,如果你想打印这个char数组中的每个字符,请使用它:username[i]
是否正确打印密码?