2013-05-29 49 views
1

我是C新手,我习惯Java OOP。我必须使用struct作为我的全局变量的持有者,系统将不断地写入/读取。struct中的多个类型 - C

我在结构中有3种不同类型的变量,我不确定是否会导致问题。

另外 - 我需要在头文件中声明结构,以便使用它的其他C文件?

在不同的函数中访问变量结构的最快和最好的方法是什么? //使用这些变量的函数位于同一个C文件中。

这里是结构本身,我提出:

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; 
    }; 

,并应使用一些结构领域的一个函数的例子:

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; 
} 

所以会是怎样的访问这些结构字段的最佳方式,而不是只写全局变量?

编辑:它是一个实时操作系统,所以任务(如线程一样)将访问结构中的数据。我不知道这是否会做出任何更改......

+0

“系统将不断写入/读取” - 如果从线程访问它,则比这更多。只需双重检查... –

+0

那么,它是一个实时操作系统,任务将访问此结构。这有什么区别吗?我会编辑我的帖子,所以每个人都可以注意到。对不起,想说明这一点。 – Milkncookiez

回答

1

是的,您应该在一个文件中定义结构,并且需要将它包含在其他文件中。结构中不同的数据类型不会有任何问题。

您可以单独定义函数来访问结构成员并对其进行处理。所以你不需要重复代码。 [就像你得到了Height()]一样。您可以定义这些功能* 内联 * 以提高性能。 (最快的方式访问?)[只有当功能简单和(小尺寸)] 即使你可以使用MACROS

为了方便,使用typedef。

typedef struct { ... } DATA; 

这样代码就可以简化了。而不是书面方式

struct Data *s; simply put DATA *s; 

你应该使用

s->structmember to process it. 
1

定义Data类型的全局变量和访问其成员,无论你想。

struct Data GlobalData; 

int getHeight() 
{ 
    GlobalData.percentage_Height = getIncline()/90.0; 
    GlobalData.speed_Height = db_speed_sensor(); 
    .... 
} 
  • 如果你想在多个文件中使用此,更好地界定在头文件中的结构。
1

您必须在头文件中声明结构。

如果这种结构的一个实例,将在全球范围内,你必须在全球范围内像这样来定义这个实例:

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

如果您的实例会在功能仅用于所宣在同一文件中,你应该定义你的变量是这样的:

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

关键字“静态”是指你的变量是在文件范围(只在文件中可见)

1

每个人都建议你使用全局结构。我只想补充一点,正如你所提到的,所有访问这个结构的函数都在一个文件中,你还应该声明一个结构为静态的,这样全局结构将是有限的文件范围。

0

一个结构体可以容纳各种类型的数据,只要你有它们的存储空间,那么在那里有多少变量没有问题。

你可以在头文件中声明一个结构体,但是你需要在某个地方初始化它。

例如,如果您将其初始化为不是主要的源文件并且您仍然想在其中使用它,则必须在主文件中使用关键字extern声明它。让我证明:

file.h - 一个struct Foo在这里被定义

file.c - 命名fooFoo实例与一些值这里初始化。

main.c - 我们想在这里使用它。为了做到这一点,我们把例如语句包括读取extern struct Foo foo;

或者你也可以简单的添加file.hmain.c文件,只是初始化它那里之后。

通常结构变量是这样访问的:name_of_struct.member,例如, struct.int_member = 42

如果你有一个指向一个struct和您尝试修改通过指针的结构也有这样做的方法有两种:

目前比较流行的方式: ptr_to_struct->member = 42;

还是一个比较典型的方式对于其他类型,但在这种情况下相当罕见(至少对于一个级别的解除引用): *ptr_to_struct.member = 42;

我建议您将结构作为参数传递给修改它的函数。不要把它当作一个普通的全球使用。

1

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.

用一个指针结构。

例子:

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; 
} 

让编译器决定何时内联这些功能来提高性能,你应该担心你的代码的可读性和组织。

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

如果您想直接访问它的其他来源的成员,则需要它在头定义,但你可以很声明您有结构在你C,并创建函数来访问你的struct的值。在这种情况下,你可以定义结构只在源文件中,并且声明它在头文件或任何其他源文件中需要这个声明。

实施例:

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; 
} 

文件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; 
} 

这将是更好具有与访问的file1结构体所需要的声明一个头,和包括这个头文件在file2中,只是用这种方式告诉你这是可能的,并且可能会给你一个关于定义声明