0

Background: I created a Hashtable in a file called hash_table.c. Here is part of what my header file hash_table.h contains:

/* 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
....

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I want to initiate the Hashtable in a different C file called fdata.c. I don't know what the best way to do this is but here is what I was thinking:

#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...
    ....

Obviously, if there is a better way to initiate this table I am all ears. (This is my first time using structures with C)

Thanks for the support!

Giga Tocka
  • 191
  • 3
  • 3
  • 11

1 Answers1

1

A global pointer will be initialized to nil on app startup. You can compare against nil and then call TableCreate.

Alternatively, you can just create the table at startup. (e.g. from main, or if you're actually writing Objective C, application:didFinishLaunchingWithOptions or something along those lines.)

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
  • What's the difference between using NULL and nil because nil gives me an error while NULL seems to be working. – Giga Tocka May 06 '13 at 00:11
  • There's no big difference in meaning. Objective C uses nil, regular C uses NULL. They both mean zero. – StilesCrisis May 06 '13 at 01:41
  • 1
    `nil` is for objects, `NULL` for non-objects! See [this SO answer](http://stackoverflow.com/questions/557582/null-vs-nil-in-objective-c) – HAS May 06 '13 at 07:34