简体   繁体   中英

C code from gcc to Visual Studio Express 2010

I'm trying to move some code I have written and compiled succesfully with gcc under Visual Studio Express 2010.

I have the following header file.

#ifndef _SYMTAB_H_
#define _SYMTAB_H_

#define NHASH 997

typedef enum {NOTYPE, INTTYPE, DOUBLETYPE, STRINGTYPE} SYMBOLTYPE;

typedef union {
    int intvalue;
    double doublevalue;
    char *stringvalue;
} SYMBOLVALUE;

typedef struct {
    SYMBOLTYPE type;
    char *name;
    SYMBOLVALUE value;
} SYMBOL;

void initSymbolTable(void);
SYMBOL *lookup(char *sym);
SYMBOL *addIntSymbol(char *name, int value);
SYMBOL *addDoubleSymbol(char *name, double value);
SYMBOL *addStringSymbol(char *name, char *value);
char *getSymbolName(SYMBOL *sym);
int getIntSymbolValue(SYMBOL *sym);
double getDoubleSymbolValue(SYMBOL *sym);
char *getStringSymbolValue(SYMBOL *sym);
void printSymbolTable(void);

#endif

If I write a piece of code using that header file such as:

int main(int argc, char *argv[]) {
    initSymbolTable();

    printSymbolTable();

    SYMBOL *intSymbol = addIntSymbol("pippo", 10);

    printSymbolTable();

    printf("All tests successfull\n");
    return 0;
}

I get the followin error:

error C2275: 'SYMBOL': illegal use of this type as an expression

which is descrideb here: http://msdn.microsoft.com/en-us/library/76c9k4ah(v=vs.71).aspx

Anyway I don'tunderstand what's wrong with that. I have also set the "Compile As" property to "Compile as C Code" in the prject properties under C/C++->Advanced. Moreover all files are saved as *.c and *.h.

You can't declare a variable mid-function in the version of C that Visual Studio supports. You need do declare it at the top:

int main(int argc, char *argv[]) {
    SYMBOL *intSymbol;

    initSymbolTable();

    printSymbolTable();

    intSymbol = addIntSymbol("pippo", 10);
    ...

Alternatively, you can right-click the .c file that throws an error in Solution Explorer, go to C/C++ -> Advanced and set Compile As to compile as C++ code. That way you don't need to edit your sources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM