如何在C中初始化结构数组?

时间:2020-08-06 06:13:41

标签: c struct arm iar

我在带有ARM处理器的开发板上进行编码,因此我将IAR WORKBENCH用作IDE。我想知道为什么编译器在训练如何手动初始化结构数组时给我一个错误,代码如下: 结构定义:

typedef void (*Function)(void);

typedef struct _Task
{
    uint8_t priority;
    Function *func;
    uint8_t exec_state;
}Task; 

尝试初始化结构数组:

Task* task_array_test[3] = {
        HIGH , &Switch_LED_RED , IDLE,
        MED , &Switch_LED_RED , IDLE,
        LOW , &Reset_LED      , IDLE
};

IDLE,LOW,MED,HIGH-具有无符号整数值的宏。

编译器错误:

Error[Pe144]: a value of type "unsigned int" cannot be used to initialize an entity of type "Task *" 
Error[Pe144]: a value of type "void (*)(void)" cannot be used to initialize an entity of type "Task *" 
Error[Pe146]: too many initializer values 

欢迎任何帮助!

5 个答案:

答案 0 :(得分:2)

首先@pmg已经提到了-类型为Task的数组就可以了,可能不需要指针数组Task*

此外,您也不需要func成为Function*类型:

typedef void (*Function)(void);

typedef struct _Task
{
    uint8_t priority;
    Function func;
    uint8_t exec_state;
}Task; 

您的数组初始化将如下所示:

Task task_array_test[] = {
        HIGH , Switch_LED_RED , IDLE,
        MED ,  Switch_LED_RED , IDLE,
        LOW ,  Reset_LED      , IDLE
};

但是我更喜欢将每个元素都放在大括号中,以便更好地阅读:

 Task task_array_test[] = {
            { HIGH , Switch_LED_RED , IDLE },
            { MED ,  Switch_LED_RED , IDLE },
            { LOW ,  Reset_LED      , IDLE }
    };

答案 1 :(得分:1)

您的代码有几个问题。

首先: 类型函数已经是一个指向函数的指针,无需再次将其声明为指针。这以指向函数指针的指针结尾。

typedef struct _Task
{
    uint8_t priority;
    Function func; // Leave out the *
    uint8_t exec_state;
}Task;

第二 初始化数组的正确方法就是这样。

Task task_array_test[] = {
    {HIGH , &Switch_LED_RED , IDLE},
    {MED , &Switch_LED_RED , IDLE},
    {LOW , &Reset_LED      , IDLE},
};

没有任务*,只有任务。空的[] allready已将其指定为数组。

这里是我编写的一个小测试程序,以证明它可以正常工作。

#include <stdint.h>
#include <string.h>
#include <iostream>
#include <stdio.h>

typedef void (*Function)(void);

static int i;

#define HIGH 10
#define MED  5
#define LOW  1
#define IDLE 1

void Switch_LED_RED(void){
   i++;
}

void Reset_LED(void){
   i++;
}

typedef struct _Task
{
    uint8_t priority;
    Function func;
    uint8_t exec_state;
}Task;

Task task_array_test[] = {
    {HIGH , &Switch_LED_RED , IDLE},
    {MED , &Switch_LED_RED , IDLE},
    {LOW , &Reset_LED      , IDLE},
};

int main(){    
    std::cout << "I starts with the value: " << i << std::endl;
    while (i < 3){
      task_array_test[i].func();  
    }
    std::cout << "I has now the value: " << i << std::endl;
    return 0;
}

答案 2 :(得分:1)

您可以使用复合文字进行初始化,如下所示:

Task* task_array_test[3] = {
        &(Task) {HIGH , Switch_LED_RED , IDLE},
        &(Task) {MED , Switch_LED_RED , IDLE},
        &(Task) {LOW , Reset_LED      , IDLE}
};

您也可以这样做:

Task s1 = {HIGH , Switch_LED_RED , IDLE};
Task s2 = {MED , Switch_LED_RED , IDLE};
Task s3 = {LOW , Reset_LED      , IDLE};

Task* task_array_test[3] = {&s1, &s2, &s3};

在结构定义中,您有func个成员声明为

Function *func;

这使func指向要键入的指针,因为Function本身就是一个指针:

typedef void (*Function)(void);

应该是:

typedef struct _Task
{
    uint8_t priority;
    Function func;       //Note that * removed
    uint8_t exec_state;
} Task; 

此外,将&分配给函数指针时,无需将其与函数名称一起使用。函数名称给出函数的地址。

答案 3 :(得分:0)

如果需要指针,可以这样做:

Task* task_array_test[] = {
    &(Task){HIGH , &Switch_LED_RED , IDLE},
    &(Task){MED , &Switch_LED_RED , IDLE},
    &(Task){LOW , &Reset_LED      , IDLE},
};

答案 4 :(得分:-3)

您尝试使用此代码

struct _Task
{
   int a,b,c;
}Task[3]; 
int main()
{
Task[0].a= HIGH;
Task[0].b= Switch_LED_RED;
Task[0].c= IDLE;
Task[1].a= MED ;
Task[1].b= Switch_LED_RED;
Task[1].c= IDLE;
Task[0].a= LOW ;
Task[0].b= Switch_LED_RED;
Task[0].c= IDLE;
}
相关问题