在C ++中将char数组解析为struct

时间:2015-10-02 14:22:22

标签: arrays pointers data-structures char

我正在使用库RF24Mesh通过nRF24l01 +在Raspberry Pi和Arduino之间进行通信。我正在尝试编写有效负载消息,但我遇到了不同的错误。

#include "RF24Mesh/RF24Mesh.h"
#include <RF24/RF24.h>
#include <RF24Network/RF24Network.h>

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>

using namespace std;

...

struct payload_t {
  char message[32];
};

...

int main(int argc, char** argv) {
  char messageSend[32];  //The combined message, ready for sending
  char splittedMessage[3][16];  //The parts to be combined

  ...

  //This is example for splitted message, it is actually read from a file
  //and the strings are not null terminated
  splittedMessage = {{"id", "func", "value"}};

  ...

  //This combines the rows of splittedMessage[][] into messageSend[]
  memcpy(messageSend, splittedMessage[1], strlen(splittedMessage[1]));
  memcpy(&messageSend[strlen(splittedMessage[1])], splittedMessage[2], strlen(splittedMessage[2]));
  memcpy(&messageSend[strlen(splittedMessage[1]) + strlen(splittedMessage[2])], splittedMessage[3], strlen(splittedMessage[3]));
  memcpy(&messageSend[strlen(splittedMessage[1]) + strlen(splittedMessage[2]) + strlen(splittedMessage[3])], "\n", 5);

  printf("messageSend: %s\n", messageSend);

  payload_t payload = { messageSend };
  RF24NetworkHeader header(4, 'M');
  network.write(header, &payload, sizeof(payload));

以上代码的错误是:

  

gateway1.cpp:76:38:错误:数组必须使用括号括起的初始化程序进行初始化

当我尝试将payload_t payload = { messageSend };更改为payload_t payload = { &messageSend };时 我明白了:

  

gateway1.cpp:76:39:警告:初始化程序周围缺少大括号   'char [32]'[-Wmissing-braces]   gateway1.cpp:76:39:错误:无效   从'char(*)[32]'转换为'char'[-fpermissive]

76是payload_t payload = { messageSend };行。

我用指针(随机的东西)尝试了不同的东西,但它不起作用。邮件发送的唯一方式是:payload_t payload = { "someMessage" };

payload_t payload = { messageSend[0], messageSend[1], messageSend[2]... };

0 个答案:

没有答案