简体   繁体   English

C:指向 function 中结构数组的指针

[英]C: Pointers to array of struct in function

I want to create a function that will enable or disable the some parts of the functions with conditions.我想创建一个 function 将根据条件启用或禁用功能的某些部分。 I have created a struct that contains 4 members (enum types, uint8 catNumber, char Name, bool flag).我创建了一个包含 4 个成员(枚举类型、uint8 catNumber、char 名称、bool 标志)的结构。

typedef struct MYSTRUCT
{
 enum modes;
 uint8 catNumber;
 char name;
 bool flag;
 }mystruct;

Enum contains:枚举包含:

typedef emum MODES {mode1, mode2, mode3}modes;

So after creating a struct template, I have declare a array variable of struct type.所以在创建了一个结构模板之后,我声明了一个结构类型的数组变量。 ie Struct mystruct variable[3].即结构mystruct 变量[3]。 And I have initialized the each member of mystruct.我已经初始化了 mystruct 的每个成员。

mystruct  Variable[3] = 
{
[0] ={.modes=mode1,
.catNumber=1,.name = “catA”,
.flag=false},

[1] = {.modes =mode2,
.catNumber=2,.name = “catB”,
.flag=false},

[2] = {.modes =mode3,
.catNumber=3,.name = “catC”,
.flag=false},
};

So the user has to enter the category number and the true/false Flag to enable or disable the part of the function ie different categories from struct and with each corresponding mode print a mode name to check it is enable.因此,用户必须输入类别编号和真/假标志来启用或禁用 function 的部分,即结构中的不同类别,并且每个相应的模式打印一个模式名称以检查它是否启用。 So the task is the user can enable one or more than one category.所以任务是用户可以启用一个或多个类别。 For eg.例如。 User enters: 1 2 true.用户输入:1 2 真。 Which enables the both categories 1 and 2.这同时启用了类别 1 和 2。

Could anyone guide me how do I do this task?谁能指导我如何完成这项任务? And is it possible not to pass the whole struct data type as an func argument.是否可以不将整个 struct 数据类型作为 func 参数传递。 I just want to declare pointers as an argument to point struct array elements in main().我只想将指针声明为指向 main() 中的结构数组元素的参数。

There are basically two ways to send a variable length array from one function to another.基本上有两种方法可以将可变长度数组从一个 function 发送到另一个。 One variant is to provide the array (basically a pointer to its first element) and the number of elements.一个变体是提供数组(基本上是指向其第一个元素的指针)和元素数。 The other option is to define a special terminating element.另一种选择是定义一个特殊的终止元素。 Just like strings in C - they are terminated with a special character with the code 0. Here my code, based on your code and some corrections.就像 C 中的字符串一样 - 它们以带有代码 0 的特殊字符终止。这是我的代码,基于您的代码和一些更正。

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

typedef enum MODES {mode1, mode2, mode3, modeTerminus} modes;

typedef struct MYSTRUCT
{
    enum MODES modes;
    int8_t catNumber;
    const char * name; //As we intend to store a string here, a simple 'char name' is insufficient
    bool flag;
} mystruct;

mystruct Variable[] =
{
    [0] ={
        .modes=mode1,
        .catNumber=1,.name = "catA",
        .flag=false
    },

    [1] = {
        .modes =mode2,
        .catNumber=2,.name = "catB",
        .flag=false
    },

    [2] = {
        .modes =mode3,
        .catNumber=3,.name = "catC",
        .flag=false
    },
    [3] = {
        .modes =modeTerminus
    },
};
void theDataProcessor1(mystruct* theList)
{
   for (const mystruct* theItem=theList; theItem->modes!=modeTerminus; theItem++)
      printf("theDataProcessor1: modes=%d catNumber=%d name=%s flag=%d\n",
              theItem->modes,
              theItem->catNumber,
              theItem->name,
              theItem->flag
            );
}
void theDataProcessor2(mystruct* theList, int Count)
{
    for (int i=0; i<Count; i++)
      printf("theDataProcessor2: modes=%d catNumber=%d name=%s flag=%d\n",
              theList[i].modes,
              theList[i].catNumber,
              theList[i].name,
              theList[i].flag
            );
}
int main()
{
    theDataProcessor1(Variable);
    theDataProcessor2(Variable, 3);
    return 0;
}

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

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