简体   繁体   English

在多个结构指针和数组中将值分配给结构指针的成员

[英]assign value to member of struct pointer, within multiple struct pointers and arrays

long title. 长标题。 but it is the exact problem i am having. 但这是我遇到的确切问题。 it may be a misunderstanding of how the struct pointer works, or a problem using tthe correct format. 这可能是对struct指针如何工作的误解,或者是使用正确格式的问题。 i have the following typedefs of structs combined in a big 'box' for easy passing around the configuration. 我将以下typedef结构体组合到一个大的“盒子”中,以便于在配置中轻松传递。

typedef struct NTC_network{
     unsigned char NET_ID;      
     unsigned char DEV_ADDR;        
}NTC_network;

typedef struct NTC_io_trx_link{
  enum NTC_IO_LINK own_io;
  unsigned char ADDR;
  enum NTC_IO_LINK trx_io;
}NTC_io_trx_link;

//struct to hold status of I/O. 0==OFF 1==ON
typedef struct NTC_config_stpio{
    unsigned char L1:1;
    unsigned char L2:1;
    unsigned char L3:1;
    unsigned char L4:1;
    unsigned char E1:1;
    unsigned char E2:1;
    unsigned char T1:1;
    unsigned char T2:1;
}NTC_config_stpio;

typedef struct NTC_header {
    unsigned char NET_ID;   
    unsigned char DST;      //destination address
    unsigned char SRC;      //source address
    unsigned char PCKT;             
}NTC_header;

typedef struct NTC_default_setup{
    unsigned char DEFAULT_SETUP:1;
}NTC_default_setup;    

typedef struct NTC_data{    
    unsigned char E1;       //resistor value to emulate on E1
    unsigned char E2;       //resistor value to emulate on E2
    unsigned char T1;       //resistor value of temperature sensor on T1
    unsigned char T2;       //resistor value of temperature sensor on T2
    unsigned char D1;       //230VAC detection circuit on/off
    unsigned char D2;       //230VAC detection circuit on/off       
    unsigned char ALRM;     //alarm on off 0xFF=alarm on, 0x00=alarm off            
}NTC_data;

typedef struct NTC_ack{
    unsigned char ACK;      //ACK field 0x00=no ack 0xFF= ack
}NTC_ack;

typedef struct NTC_config {
    NTC_default_setup *DEFAULT_STP;
    NTC_config_stpio *STPIO;
    NTC_config_network *NETWORK;
    NTC_io_trx_link *NTC_link_io;               
}NTC_config;

//struct for payload to send via LoRa
typedef struct NTC_payload{
    NTC_header *Header;
    enum NTC_dc DC; 
    NTC_config *Config;
    NTC_data *Data;
    NTC_ack *Ack;   
}NTC_payload;

typedef struct NTC_runtime_data{
    NTC_payload *pPayload;  
}NTC_runtime_data; 

and allocating the whole thing at runtime as such: 并在运行时按如下方式分配整个内容:

NTC_runtime_data* init_struct_runtime_data(void){   

    NTC_runtime_data *DATA = calloc(1, sizeof(NTC_runtime_data));   

    DATA->pPayload = calloc(2, sizeof(NTC_payload));
    for(int i = 0 ; i < 2; i++){
        DATA->pPayload[i].Header = calloc(1, sizeof(NTC_header));
        DATA->pPayload[i].DC = NONE; 
        DATA->pPayload[i].Config = calloc(1, sizeof(NTC_config));
        DATA->pPayload[i].Config->DEFAULT_STP = calloc(1, sizeof(NTC_default_setup));
        DATA->pPayload[i].Config->STPIO = calloc(1, sizeof(NTC_config_stpio));      
        DATA->pPayload[i].Config->NETWORK = calloc(1, sizeof(NTC_config_network));
        DATA->pPayload[i].Config->NETWORK->NTC_NETWORK = calloc(1, sizeof(NTC_network));
        DATA->pPayload[i].Config->NTC_link_io = calloc(11, sizeof(NTC_io_trx_link));
        for(int j = 0; j < 11; j++){
            DATA->pPayload[i].Config->NTC_link_io[j].own_io = j + 1;
        }           
        DATA->pPayload[i].Data = calloc(1, sizeof(NTC_data));
        DATA->pPayload[i].Ack = calloc(1, sizeof(NTC_ack));
    }
    return DATA;
}

i am allocating 2 pPayload. 我正在分配2 pPayload。 one for TX data and one for RX. 一个用于TX数据,另一个用于RX。 all this works fine and there is no problem what so ever assigning values like so: 所有这些都可以正常工作,并且像这样分配值没有问题:

    NTC_runtime_data *pDATA;
    pDATA = init_struct_runtime_data();

and then where ever i might pass this struct i can assign as follows: 然后我可以在任何地方通过此结构,我可以分配如下:

pData->pPayload[0].Header->NET_ID = 0x20;

but i am having problems with trying to assign from a struct containing an array as a buffer which looks like this: 但是我在尝试从包含数组的结构中分配作为缓冲区的问题时看起来像这样:

typedef struct TRX_buffer{
    size_t used;
    size_t size;
    unsigned char *array;
}TRX_buffer;

and initializing and reallocating as so: 并如此初始化和重新分配:

TRX_buffer* init_struct_trx_buffer(void){
    TRX_buffer *trx_buffer = malloc(sizeof(TRX_buffer));
    trx_buffer->array = malloc(4 * sizeof(unsigned char));
    trx_buffer->size = 4;
    trx_buffer->used = 0;
    return trx_buffer;
}
//inserts a byte of data in the buffer and dynamically allocates the buffer
void insert_array(TRX_buffer *trxbuffer, unsigned char data){
    if(trxbuffer->used == trxbuffer->size){
        trxbuffer->size *= 2;
        trxbuffer[0].array = realloc(trxbuffer[0].array, trxbuffer->size * sizeof(unsigned char));
    }
    trxbuffer[0].array[trxbuffer->used] = data;
    trxbuffer->used++;
}

so the problem is, some where in my program ,in a function, where i am creating a new instance of the buffer and load data from the radio, which i can see when debugging, i want to load it to my struct and i do it like this 所以问题是,在程序的某个位置,函数中,我正在创建缓冲区的新实例并从无线电中加载数据,在调试时可以看到它,我想将其加载到我的结构中,然后执行像这样

NTC_payload* get_payload(NTC_payload *pPayload){    
    TRX_buffer *rx_buffer;
    rx_buffer = init_struct_trx_buffer();

    r_FIFO(rx_buffer);

    pDATA->pPayload[1].Header->NET_ID = trx_buffer->array[0];

the correct data is in the trx_buffer after my r_FIFO() function. 正确的数据在trx_buffer我后r_FIFO()函数。 but this isn't working. 但这不起作用。 i have tried different methods. 我尝试了不同的方法。 but cant get it to take the value from my buffer and assign it to my struct. 但无法获取它以从我的缓冲区中获取值并将其分配给我的结构。

what is wrong?? 怎么了??

You are not returning anything from init_struct_runtime_data(), so pDATA = init_struct_runtime_data(); 您没有从init_struct_runtime_data()返回任何内容,因此pDATA = init_struct_runtime_data(); gets whatever happened to be in the return register. 获取在返回寄存器中发生的任何事情。

Any decent compiler should flag this, if yours doesn't do yourself a favour and uninstall it. 如果您的安装程序不能帮自己一个忙,那么任何不错的编译器都应标记此选项并卸载它。

ps: there is a faq around here somewhere that talks about posting complete programs so people can try them with their tools. ps:这里附近有一个常见问题,谈论发布完整的程序,以便人们可以使用他们的工具进行尝试。 It is a good idea, because simply reducing the problem to a publishable version reveals the problem. 这是一个好主意,因为仅将问题简化为可发布的版本即可解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM