2013-05-05 59 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