1

私はCにまったく慣れていません。他のリンクを試しましたが、サイコロはありません。
Xcode 6.1 を使用していますが、次の問題が発生します。

解析の問題: 予期される識別子または '('
セマンティックの問題: 予期しない型名 'map'

これが私のコードです:

//hashmap.h  
#ifndef __HashMap__hashmap__
#define __HashMap__hashmap__

void map_init();
void map_insert(uint8_t, uint8_t);
uint8_t map_getVal(uint8_t);

#endif /* defined(__HashMap__hashmap__) */  

//hashmap.c
#include <stdint.h>
#include "hashmap.h"

#define KEY_NOT_FOUND   -1

static int i = 0;

typedef struct HashMap {
    uint8_t KEY;
    uint8_t VAL;
} *map;

void map_init() {
    map = (HashMap*) calloc(1, sizeof(HashMap));                //Parse Issue
}

void map_insert(uint8_t key, uint8_t val) {
    int size;
    map[i].KEY = key;                                           //Parse Issue
    map[i].VAL = val;                                           //Parse Issue
    i++;
    size = i + 1;
    map = (HashMap*) realloc(map, size * sizeof(HashMap));      //Parse Issue
}

int map_search(HashMap map[], uint8_t key) {
    int index, size = i;
    for(index = 0; index <= size; index++)
        if(map[index].KEY == key)
            return index;
    return KEY_NOT_FOUND;
}

uint8_t map_getVal(uint8_t key) {
    return map[map_search(map, key)].VAL;                       //Semantic Issue
}

map[i]をポインター配列表記(map + i)に置き換えてみましたが、同じ結果が得られました。また、問題が修正された後にハッシュマップがどのように失敗するかを指摘してください.

4

2 に答える 2

1

変化する:

typedef struct HashMap {
    uint8_t KEY;
    uint8_t VAL;
} *map;

と:

typedef struct HashMap {
    uint8_t KEY;
    uint8_t VAL;
} HashMap ;

HashMap *map;
于 2015-06-19T20:42:53.643 に答える