Getting a Expected identifier or '(' issue in Xcode 6.1

276 views Asked by At

I'm quite new to C. I've tried other links but no dice.
I'm using Xcode 6.1 and I get the following issues:

Parse Issue: Expected identifier or '('
Semantic Issue: Unexpected type name 'map'

Here's my code:

//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
}

I tried replacing map[i] with the pointer array notation (map + i) yields the same result. Also feel free to point out how my hashmap will flop after the issues are fixed.

2

There are 2 answers

1
ouah On BEST ANSWER

Change:

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

with:

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

HashMap *map;
2
nanofarad On

The typedef:

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

defines a new type called *mapEntry. You can then create an actual variable:

mapEntry map;

Now, the rest of your code should be parsable as map refers to the variable and not the type.