开发者

typedefs of structs not seeming to go through in header files?

I'm having some trouble with some struct typedef declarations in a header file not seeming to go through to my implementation file.

Specifically, I have the following types defined: Type, Value, Integer, String, and Float. They are all typedef'd from struct names, in the exact same manner. I'm writing an informal hashCode function to supplement a hashtable I'm designing that references all of these data types. Type and Value work okay, but Integer/Float/String do not work properly and I can't figure out why.

I apologize, this question is a little involved, but I'll try not to provide too much or too little information and maybe this won't be too difficult for the experts here. :-)

I'll start with the hashCode function (and please don't give me flak about how crappy it is, I know it's not all that great and I really don't care):

int hashCode(ST_HashSymbol *hash, Value *v)
{
        Type *t = v->type;

        switch (whichType(t->name))
        {
                case INTEGER:
                        Integer *i = (Integer *)v->innerValue;
                        return i->value % hash->capacity;

        case FLOAT:
        {
            Fl开发者_运维问答oat *f = (Float *)v->innerValue;
            float val = f->value;
            long l = 0l;

            if (val  2 && j = 0; --j)
            {
                if (val >= pow(2, j - 22))
                {
                    val -= pow(2, j - 22);
                    l |= 1 capacity;
        }

                case STRING:
                        String *s = (String *)v->innerValue;
                        char *str = s->value;

                        int total = 0;

                        char *c;
                        for (c = str; *c != '\0'; ++c)
                        {
                                total += *c;
                        }

                        return total % hash->capacity;

                default:
                        return -1;
        }
}

Excerpt from the "type.h" header file, which defines all of the types. It's worth noting that I've also tried having the typedef and the struct definition combined as one statement, but that didn't work either:

typedef struct _t Type;
typedef struct _v Value;

struct _t {
        char *name;
        struct _t *widerType;
};

struct _v {
        Type *type;
        void *innerValue;
};

Type *type(int);
int whichType(char *);
Type *getType(char *);

/**************************/
/* Actual ("inner") types */
/**************************/

typedef struct _str String;
typedef struct _int Integer;
typedef struct _fl Float;

struct _str {
        int length;
        char *value;
};

struct _int {
        int value;
};
struct _fl {
        float value;
};

When I run make, I get the following:

[kparting@dhcp-10-25-247-130 eq]$ make
gcc -o eq -Wall -g parser.c eq.c error.c hash.c symbols.c type.c -lm
hash.c: In function ‘hashCode’:
hash.c:33: error: expected expression before ‘Integer’
hash.c:34: error: ‘i’ undeclared (first use in this function)
hash.c:34: error: (Each undeclared identifier is reported only once
hash.c:34: error: for each function it appears in.)
hash.c:37: error: expected expression before ‘Float’
hash.c:38: error: ‘f’ undeclared (first use in this function)
hash.c:69: error: expected expression before ‘String’
hash.c:70: error: ‘s’ undeclared (first use in this function)
make: *** [eq] Error 1

As I mentioned, Type * and Value * are valid data types, but the other three aren't. The whichType and type functions do not use any of the other three data types.

Thanks in advance for any help. I'm fairly sure this has to do either with the location of the structs within the header file, or possibly (but highly unlikely) gcc itself.


You can't declare a variable inside a case block.

That's not entirely true actually. See here. Should help you clear things up.


In C you can only declare a variable at the start of a block. Your line:

Integer *i = (Integer *)v->innerValue;

tries to declare a variable i, but it is not at the start of a block. You can fix this just by opening a block:

case INTEGER:
{
    Integer *i = (Integer *)v->innerValue;
    return i->value % hash->capacity;
}

...and similar for the other cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜