T
thomas.luce
Okay, I have been programming for a long time, and am getting back into
C after about a 4 year break from it. Below is some code that won't
compile for the life of me, and it is driving me crazy! If anyone can
see anything that I can't, I would love to know. All the code is
pasted, although in my real code it is broken into a header and a
seperate .c file.
#include <stdlib.h>
typedef enum {typeNum, typeString} VariableType;
typedef struct variableTag {
int id;
VariableType type;
union {
double nValue;
char *sValue;
};
} Variable;
#define VAR_SIZE = sizeof(int) + sizeof(VariableType) + sizeof(double)
+ sizeof(char*);
int num_vars = 0;
Variable *my_vars;
my_vars = (Variable *)malloc(100 * sizeof(Variable));
/*
* Make a variable and return a pointer to it.
*/
struct Variable *make_var(char *name) {
//TODO: do this.
}
Seems simple, right? (this is part of a lex/yacc compiler I am working
on). When I compile it, here is what I get:
file.c:6: error: conflicting types for 'my_vars'
file.c:5: error: previous declaration of 'my_vars' was here
file.c:6: warning: initialization makes integer from pointer without a
cast
file.c:6: error: initializer element is not constant
file.c:6: warning: data definition has no type or storage class
Just for a sanity check I tried this:
int *p;
p = malloc(sizeof(int) * 100);
Same errors. Am I going crazy? Is there something horribly simple that
I have forgotten about C in the last few years?
Any help is much appreciated!
Thanks,
-Thomas
C after about a 4 year break from it. Below is some code that won't
compile for the life of me, and it is driving me crazy! If anyone can
see anything that I can't, I would love to know. All the code is
pasted, although in my real code it is broken into a header and a
seperate .c file.
#include <stdlib.h>
typedef enum {typeNum, typeString} VariableType;
typedef struct variableTag {
int id;
VariableType type;
union {
double nValue;
char *sValue;
};
} Variable;
#define VAR_SIZE = sizeof(int) + sizeof(VariableType) + sizeof(double)
+ sizeof(char*);
int num_vars = 0;
Variable *my_vars;
my_vars = (Variable *)malloc(100 * sizeof(Variable));
/*
* Make a variable and return a pointer to it.
*/
struct Variable *make_var(char *name) {
//TODO: do this.
}
Seems simple, right? (this is part of a lex/yacc compiler I am working
on). When I compile it, here is what I get:
file.c:6: error: conflicting types for 'my_vars'
file.c:5: error: previous declaration of 'my_vars' was here
file.c:6: warning: initialization makes integer from pointer without a
cast
file.c:6: error: initializer element is not constant
file.c:6: warning: data definition has no type or storage class
Just for a sanity check I tried this:
int *p;
p = malloc(sizeof(int) * 100);
Same errors. Am I going crazy? Is there something horribly simple that
I have forgotten about C in the last few years?
Any help is much appreciated!
Thanks,
-Thomas