0

I want to create a function that will enable or disable the some parts of the functions with conditions. I have created a struct that contains 4 members (enum types, uint8 catNumber, char Name, bool flag).

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. i.e. Struct mystruct variable[3]. And I have initialized the each member of 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 i.e different categories from struct and with each corresponding mode print a mode name to check it is enable. So the task is the user can enable one or more than one category. For eg . User enters: 1 2 true. Which enables the both categories 1 and 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. I just want to declare pointers as an argument to point struct array elements in main().

1 Answer 1

1

There are basically two ways to send a variable length array from one function to another. 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.

#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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to indirectly access each elements of struct array with pointers without declaring it as struct type?
I don't understand why you want to avoid a struct. In your OP you write that the user provides a category and a boolean flag for that category. Why not glue them together and pack them in a struct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.