2014-02-24 56 views
0

有4個.c/.h文件。每個變量都有一些全局變量&結構變量。我從.map文件中獲得所有變量名稱。我提取所有在二維數組/數組[](char類型)。現在我想傳遞sizeof每個變量和地址是否可以在運行時查找結構變量和全局變量的sizeof和地址?

+0

你能介紹一下你的問題嗎? –

+0

@SouravGhosh:考慮你有一些。 c/.h文件。你必須傳遞所有變量的參數大小和變量地址(全局和結構)。所以我得到一個考慮所有變量名稱的.map文件。我從這個文件存儲緩衝區中提取變量名稱。通過使用緩衝區來傳遞變量的地址和其大小,任何方式都是可行的 –

+0

您是否需要在.map文件中分配具有大小內容的變量大小?我明白嗎?你的問題很混亂。 – GabrielBiga

回答

0

好的。如果您需要在變量中分配動態內存,則需要在「stdlib.h」中使用malloc函數並讀取該文件,您可以將其存儲到鏈接列表中以逐個進行處理。

檢查的例子... 我們在這個語義一個「variables.map」:

eua 40 
brasil 30 
paris 15 
horse 8 

其中第一列是名稱(最多40個字符見結構原型)可變的和第二個是尺寸。 (注:以換行符'\ n'分隔)

程序將文件加載到內存中的鏈接列表中並分配各自的內存空間(Check main())。

參見示例程序...

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

//MAP prototype 
typedef struct map { 
    char name[40]; 
    unsigned int size; 

    struct map *next; 
} map; 
//--- 

//Prototypes 
void createList(); 
void insert(char[], int, map*); 
void showList(map*); 
unsigned int searchVariable(char[], map*); 
void parseMapFile(char[]); 
//--- 

//List, Linked list pointer 
map *list_variables; 
//--- 

//Main! 
int main(int argc, const char * argv[]) 
{ 
    //Create list! 
    createList(); 

    //Parse the .Map file into struct 
    parseMapFile("/Users/gabriel/Documents/variables.map"); 

    //Show linked list 
    //showList(list_variables); 

    //------ 
    //Lets go now allocate the variables with the size in .map file 

    //Allocate space! 
    //Tipical types 
    int *eua = malloc(searchVariable("eua", list_variables) * sizeof(int)); 
    char *brasil = malloc(searchVariable("brasil", list_variables) * sizeof(char)); 
    float *paris = malloc(searchVariable("paris", list_variables) * sizeof(float)); 

    //Alloc the void type (Undefined) 
    void *horse = malloc(searchVariable("horse", list_variables) * sizeof(void)); 
    //--- 

    //Set values 
    *eua = 5; //Integer 
    strcpy(brasil, "Cool!"); //String 
    *paris = 3.14; //Float 
    *(int*)horse = (int) 7; //Set a integer value to void pointer! 
    //--- 

    //Show up! 
    printf("Variable eua: %d \n", *eua); 
    printf("Variable brasil: %s \n", brasil); 
    printf("Variable paris: %f \n", *paris); 
    printf("Variable horse: %d \n", *((int*)horse)); 

    return EXIT_SUCCESS; 
} 


//Linked list functions... 
//Allocate the linked list on memory 
void createList() { 
    list_variables = malloc(sizeof (map)); 
    list_variables->next = NULL; 
} 

//Insert the .MAP values into linked list 
void insert(char name[], int size, map *p) 
{ 
    map *new; 

    new = malloc(sizeof (map)); 

    new->size = size; 
    strcpy(new->name, name); 
    new->next = p->next; 
    p->next = new; 
} 

//Show variables loaded from .MAP file 
void showList(map *list) 
{ 
    map *p; 
    for (p = list->next; p != NULL; p = p->next) 
     printf("Variable: %s, Size: %d \n", p->name, p->size); 
} 

//Search variable in memory and return the size respective 
unsigned int searchVariable(char name[], map *list) 
{ 
    map *p; 
    p = list->next; 

    while (p != NULL && strcmp(name, p->name) != 0) 
     p = p->next; 

    return p->size; 
} 
//--- 

//Procedure to parse the map file in the specified structure! 
void parseMapFile(char path[]) { 
    char line[80]; 
    char name[40]; 
    unsigned int size; 

    FILE *fp = fopen(path, "r"); 

    while(fgets(line, 80, fp) != NULL) 
    { 
     sscanf (line, "%s %d", name, &size); 

     insert(name, size, list_variables); 
    } 

    fclose(fp); 
} 

有了這個,你真的可以存儲在一個文件中的值分配動態空間。

+0

@GabrieBiga:嗨,我還有一個問題。我的問題是一個文件具有所有的全局變量和結構變量。我在每次讀取每個變量。我想將該全局變量/結構變量的地址傳遞給一個函數。這是可能的。如果可能,那麼如何? –