2013-05-29 50 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中,只是用這種方式告訴你這是可能的,並且可能會給你一個關於定義聲明