2013-05-05 124 views
0

背景:我在一個名爲hash_table.c的文件中創建了一個Hashtable。這裏是什麼我的頭文件hash_table.h包含部分:檢查結構是否存在? [C]

/* hash_table.h */ 
#ifndef HASH_TABLE_H 
#define HASH_TABLE_H 

enum {BUCKET_COUNT = 1024}; 

struct Node { 
    char *key; 
    char *value; 
    struct Node *next; 
}; 

struct Table { 
    struct Node *array[BUCKET_COUNT]; 
}; 

struct Table *TableCreate(void); //creates the table 
.... 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ 我想在一個名爲fdata.c的不同C文件中啓動Hashtable。我不知道該怎麼辦最好的方法是但這裏是我的想法是:

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

struct Table *t; //initiate the variable 

void testFunc() 
{ 
    if(Hashtable does not exist) { //How do I accomplish this? Is it possible? 
     t = TableCreate(); //create the table 
    else{ 
     continue... 
    .... 

顯然,如果有更好的方法來啓動這個表,我所有的耳朵。 (這是我第一次用C結構)

感謝您的支持!

回答

1

在應用程序啓動時,全局指針將被初始化爲nil。您可以與nil進行比較,然後致電TableCreate

或者,您可以在啓動時創建表。 (例如,從main,或者如果你實際上正在編寫Objective C,application:didFinishLaunchingWithOptions或其他東西)。

+0

使用NULL和nil有什麼區別,因爲nil在NULL似乎正在工作時給我一個錯誤。 – 2013-05-06 00:11:01

+0

意義沒有太大的區別。目標C使用零,常規的C使用NULL。他們都意味着零。 – StilesCrisis 2013-05-06 01:41:50

+1

'nil'用於對象,'NULL'用於非對象!看到[這個SO答案](http://stackoverflow.com/questions/557582/null-vs-nil-in-objective-c) – HAS 2013-05-06 07:34:31