在另一个sruct中分配一个结构数组

时间:2015-11-07 19:05:57

标签: c arrays multidimensional-array struct

我有以下代码:

.user_register label {display: block; margin-bottom:5px;}
.user_register input[type="text"], .user_register input[type="email"], .user_register input[type="password"] {display: block; width:90%; padding: 10px; border:1px solid #DDD; color:#666;}
.user_register input[type="checkbox"] {float:left; margin-right:5px;}
.user_register input[type="checkbox"]+label {float:left;}

.user_register .checkbox {margin-bottom: 10px; clear: both; overflow: hidden;}

/* User Login Form */
.user_login {display: block;}
.user_login label {display: block; margin-bottom:5px;}
.user_login input[type="text"], .user_login input[type="email"], .user_login input[type="password"] {display: block; width:90%; padding: 10px; border:1px solid #DDD; color:#666;}
.user_login input[type="checkbox"] {float:left; margin-right:5px;}
.user_login input[type="checkbox"]+label {float:left;}

.user_login .checkbox {margin-bottom: 10px; clear: both; overflow: hidden;}
.forgot_password {display:block; margin: 20px 0 10px; clear: both; overflow: hidden; text-decoration: none; color:#ED6347;}


.btn {padding:10px 20px; background: #F4F4F2;}
.btn_red {background: #ED6347; color: #FFF;}

.btn:hover {background: #E4E4E2;}
.btn_red:hover {background: #C12B05;}

a.btn {color:#666; text-align: center; text-decoration: none;}
a.btn_red {color: #FFF;}

.one_half {width:50%; display: block; float:left;}
.one_half.last {width:45%; margin-left:5%;}

正如您所看到的,struct sensor_data{ uint8_t data_type; signed int data_left; uint8_t data_right; }; struct sensor_payload{ uint16_t key, id, type; uint8_t fields; struct sensor_data data[4]; bool valid; }; 内部有struct sensor_payload。基本上,结构中的结构数组。

当我尝试分配它时:

struct sensor_data[4]

我得到: uint16_t rcvd_key = (buf[0] << 8) | buf[1]; uint16_t rcvd_id = (buf[2] << 8) | buf[3]; uint16_t rcvd_type = (buf[4] << 8) | buf[5]; uint8_t rcvd_fields = buf[6]; struct sensor_data rcvd_data[4]; while(i <= (rcvd_fields)){ rcvd_data[i].data_type = buf[j+7]; rcvd_data[i].data_left = buf[j+8]; rcvd_data[i].data_right = buf[j+9]; i++; j = j + 3; } struct sensor_payload payload; payload.key = rcvd_key; payload.id = rcvd_id; payload.type = rcvd_type; payload.data = rcvd_data; <<LINE WITH ERROR payload.valid = true;

甚至不确定错误的含义......

我试过了:

assignment to expression with array type

不改变任何其他代码,并编译,但我担心它不是最干净的方式......或者正确的方式......

我觉得这个问题是由于我没有完全理解指针,或者在这种情况下没有看到它们的使用。

2 个答案:

答案 0 :(得分:3)

无法使用赋值运算符=复制数组,因此无法将它们分配给。
使用

  1. A for - 循环:

    for (size_t i = 0; i < sizeof(payload.data) / sizeof(*(payload.data)); ++i)
        payload.data[i] = rcvd_data[i];
    
  2. memcpy

    memcpy(payload.data, rcvd_data, sizeof(payload.data));
    
  3. 代替。

答案 1 :(得分:1)

您可以节省一些时间和麻烦,但将数据直接写入有效负载结构,如此

struct sensor_payload payload;

payload.key    = (buf[0] << 8) | buf[1];
payload.id     = (buf[2] << 8) | buf[3];
payload.type   = (buf[4] << 8) | buf[5];
payload.fields =  buf[6];

j = 7;
for ( i = 0; i < 4 && i < payload.fields; i++ )
{
    payload.data[i].data_type  = buf[j++];
    payload.data[i].data_left  = buf[j++];
    payload.data[i].data_right = buf[j++];
}

payload.valid = true;