Question

I'm writing some code, when I'm trying to test my code till now, I get an error.

Here is my code:

#include <stdio.h>

enum { add = 0, addu, sub, subu } mips_opcode;
typedef enum mips_opcode mips_opcode_t;

typedef unsigned char byte; // 8-bit int

struct mips {
    char *name;
    byte opcode;
};
typedef struct mips mips_t;

void init (mips_t *out, char *name_tmp, mips_opcode_t opcode_tmp) {
    out->name = name_tmp;
    out->opcode = (byte)opcode_tmp;
}

int main (void) {
    pritnf("no error i assume\n");

    return 0;
}

and the error in the commmand-line is:

main.c:14:55: error: parameter 3 ('opcode_tmp') has incomplete type

Can't I use enums as parameter or what am I doing wrong here?

Was it helpful?

Solution

It needs to be like this:

enum mips_opcode { add = 0, addu, sub, subu }; // type name is "enum mips_opcode"
typedef enum mips_opcode mips_opcode_t;        // type alias

Or even:

typedef enum { add = 0, addu, sub, subu } mips_opcode_t; // alias of anon. type

Don't confuse type names and variables!

(By the way, Posix reserves _t suffixes for types, I believe...)

OTHER TIPS

This line is the culprit:

enum { add = 0, addu, sub, subu } mips_opcode;

You're declaring a variable called mips_opcode, of an anonymous enum type.

It should read:

enum mips_opcode { add = 0, addu, sub, subu };

The name of the enum list goes right after the word enum.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top