2014-02-24 34 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:嗨,我还有一个问题。我的问题是一个文件具有所有的全局变量和结构变量。我在每次读取每个变量。我想将该全局变量/结构变量的地址传递给一个函数。这是可能的。如果可能,那么如何? –