Milkncookiez
Milkncookiez

Reputation: 7407

multiple types in struct - C

I am new to C, and I am used to Java OOP. I have to use struct as holder for my global variables, that the system will write to/read from constantly.

I have 3 different types of variables in the struct, and I am not sure if that can cause a problem.

Also - do I need to declare the struct in the header file, in order to use it other C file ?

What will be the fastest and better way to access the variables from that struct in the different functions ? // The functions using these variables are in the same C file.

Here is the struct itself, that I made:

struct Data
    {
      double percentage_Height = 0;
      double speed_Height = 0;
      double time_Height = 0;
      int distance_Height = 0;
      double percentage_Distance = 0;
      double speed_Distance = 0;
      double time_Distance = 0;
      int distance_Distance = 0;
      uint16_t valueSpeed = 0;
      double speedOfMotor = 0;
      int tilt = 0;
    };

and an example of a function that should use some of the fields of the struct:

int getHeight()
{
    percentage_Height = getIncline()/90.0;
    speed_Height = db_speed_sensor();
    time_Height = 0.000027;                 // [h] , 100ms
    distance_Height=0;
    if (percentage_Height == 0)
    {
        percentage_Height = 1;
    }
    distance_Height = speed_Height * time_Height * percentage_Height * 100;
    return distance_Height;
}

so what will be the best way to access these struct fields, instead of writing just the global vars?

EDIT: it is a real-time operating system, so tasks (smth like threads) will be accessing the data in the struct. I don't know if that makes any changes...

Upvotes: 1

Views: 3943

Answers (6)

Jonatan Goebel
Jonatan Goebel

Reputation: 1149

What will be the fastest and better way to access the variables from that struct in the different functions ? // The functions using these variables are in the same C file.

Using a pointer to that struct.

Example:

int getHeight(struct Data *data)
{
    data->percentage_Height = getIncline()/90.0;
    data->speed_Height = db_speed_sensor();
    data->time_Height = 0.000027;                   // [h] , 100ms
    data->distance_Height=0;
    if (data->percentage_Height == 0)
    {
        data->percentage_Height = 1;
    }
    data->distance_Height = data->speed_Height * data->time_Height * data->percentage_Height * 100;
    return data->distance_Height;
}

int main(int argc, char *argv[])
{
    struct Data data;

    load_data(&data); // function to initialize data

    distance_Height = getHeight(&data);

    return distance_Height;
}

Let the compiler decides when to inline those functions to improve performance, you should be worried about the readability and organization of your code.

Also - do I need to declare the struct in the header file, in order to use it other C file ?

If you want to direct access its members in other sources, you need to define it in a header, but you could just declare that you have that struct in you C, and create function to access the values of you struct. In this case you could define the struct only in the source file, and declare it in a header or any other source files you need this declaration.

Example:

file1:

#include <stdlib.h>

struct my_struct_s {
    int value;
};

struct my_struct_s *create_my_struct(void)
{
    return malloc(sizeof(struct my_struct_s));
}

void destroy_my_struct(struct my_struct_s *my_struct)
{
    free(my_struct);
}

void set_my_struct(struct my_struct_s *my_struct, int value)
{
    my_struct->value = value;
}

int get_my_struct(struct my_struct_s *my_struct)
{
    return my_struct->value;
}

file 2:

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

struct my_struct_s;
struct my_struct_s *create_my_struct(void);
void destroy_my_struct(struct my_struct_s *my_struct);
void set_my_struct(struct my_struct_s *my_struct, int value);
int get_my_struct(struct my_struct_s *my_struct);


int main() {
    struct my_struct_s *my_struct;
    my_struct = create_my_struct();
    set_my_struct(my_struct, 10);
    printf("my_struct value = %d\n", get_my_struct(my_struct));
    destroy_my_struct(my_struct);
    return 0;
}

It would be better to have a header with the declarations needed to access the struct of file1, and include this header in file2, just made it this way to show you that it is possible, and maybe give you a clue about the difference between definition and declaration

Upvotes: 1

Nobilis
Nobilis

Reputation: 7458

A struct can hold any variety of types, no problem with how many variables you have in there as long as you have the memory for them.

You can declare a struct in a header file, but you will need to initialise it somewhere.

For example if you initialise it in a source file that is not your main and you still want to use it there, you will have to declare it in the main file with the keyword extern. Let me demonstrate:

file.h - a struct Foo is defined here

file.c - an instance of Foo named foo is initialised here with some values.

main.c - we want to use it here. In order to do that we put a statement for example right after the includes that reads extern struct Foo foo;

Or you can plain add file.h to your main.c file and just initialise it there.

Normally a struct variable is accessed like this: name_of_struct.member, e.g. struct.int_member = 42.

If you have a pointer to a struct and you try to modify the struct via the pointer there are two ways of doing this:

The more popular way: ptr_to_struct->member = 42;

Or a more typical way for other types, but rather unusual in this case (at least for one level of dereference): *ptr_to_struct.member = 42;

I would recommend that you pass the struct as an argument to the functions modifying it. Don't just use it as a plain global.

Upvotes: 0

nurd_droid
nurd_droid

Reputation: 64

Everyone has advised you to use a global structure. I would just like to add that, as you mentioned, all the functions accessing this structure are in one file, you should also declare a structure as static so that global structure will be limited file scope.

Upvotes: 1

Dineshkumar
Dineshkumar

Reputation: 4245

Yes, you should define the struct in a file and need to include it for its usage in other files. different datatypes in structure won't have any issue.

You can define functions separately to access the structure members and to process it. so that you don't need to repeat code. [ as like you did getHeight() ]. you can define those functions *inline* to improve performance. (fastest way to access?) [only if the function is simple and (small in size)] even you can use MACROS.

Use typedef for convenience.

  typedef struct { ... } DATA;

so that code can be simplified. instead of writting

  struct Data *s;  simply put DATA *s;

you should use

  s->structmember to process it.

Upvotes: 1

nouney
nouney

Reputation: 4411

You have to declare the structure in a header file.

If an instance of this structure will be in the global scope, you have to define this instance in the global scope like this:

struct Data gData;
int getHeight()
{
    gData.percentage_Height = getIncline()/90.0;
    ...
}

If your instance will be used only in the functions which are declared in the same file, you should define your variable like this:

static struct Data gData;
int getHeight()
{
    gData.percentage_Height = getIncline()/90.0;
    ...
}

The keyword "static" means your variable is in the file scope (only visible in the file)

Upvotes: 1

Rohan
Rohan

Reputation: 53386

Define a global variable of type Data and access its members wherever you want.

struct Data GlobalData;

int getHeight()
{
    GlobalData.percentage_Height = getIncline()/90.0;
    GlobalData.speed_Height = db_speed_sensor();
    ....
}
  • If you want to use this in multiple files, better to define the structure in header file.

Upvotes: 1

Related Questions