Reputation: 161
I cannot for the life of me figure why my code isn't compiling. I am using an up to date gcc version to compile and this code is in the bottom part of a lex file (so it is copied directly into a .c file after using flex on it) which makes me 90% sure that this problem is due to something I am doing wrong in C. Here is the code which is causing problems:
void checkAliases() {
if (aliasHead==0) {
printf("No aliases have been created.");
}
else {
struct AliasNode* current = aliasHead;
printf("Current Aliases: \n");
while (current!=0) {
printAlias(current);
current = current->next;
}
}
struct AliasNode {
struct AliasNode* next;
char* key;
char* value;
};
struct AliasNode* aliasHead = 0;
void printAlias(struct AliasNode* alias) {
char* toPrint = alias->key;
printf(toPrint);
printf(": ");
toPrint = alias->value;
printf(toPrint);
printf("\n");
}
Here are the errors I am getting:
error: ‘aliasHead’ undeclared (first use in this function)
if (aliasHead==0) {
^
error: unknown type name ‘AliasNode’
AliasNode* current = aliasHead;
error: request for member ‘next’ in something not a structure or union
current = current->next;
^
error: unknown type name ‘AliasNode’
AliasNode* next;
^
This C code (along with thousands of lines more that I left out) is generated by this yacc file:
%token CD BYE ALIAS
%%
program: /*empty*/
| program command
;
command:
cd|bye|alias
cd:
CD {printf("Not a valid directory\n"); }
;
bye:
BYE {exit(0); }
;
alias:
ALIAS {
if (aliasHead==0) {
printf("No aliases have been created.");
}
else {
AliasNode* current = aliasHead;
printf("Current Aliases: \n");
while (current!=0) {
printAlias(current);
current = current->next;
}
}
}
;
%%
/*global variables*/
/*linked list to store alias - command pairs */
struct AliasNode {
AliasNode* next;
char* key;
char* value;
};
struct AliasNode* aliasHead = 0;
void printAlias(struct AliasNode* alias) {
char* toPrint = alias->key;
printf(toPrint);
printf(": ");
toPrint = alias->value;
printf(toPrint);
printf("\n");
}
I am absolutely lost as to how to fix these errors. Any help at all, even if its just a nudge in the right direction, is greatly appreciated. Thanks in advance!
Edit:
I changed my code in the Yacc file so that now when I put it through Bison, the .c I get out has this:
struct AliasNode {
AliasNode* next;
char* key;
char* value;
};
struct AliasNode* aliasHead = 0;
void printAlias(struct AliasNode* alias) {
char* toPrint = alias->key;
printf(toPrint);
printf(": ");
toPrint = alias->value;
printf(toPrint);
printf("\n");
}
void aliasNoPrompt() {
if (aliasHead==0) {
printf("No aliases have been created.");
}
else {
AliasNode* current = aliasHead;
printf("Current Aliases: \n");
while (current!=0) {
printAlias(current);
current = current->next;
}
}
However, I am still running into the same errors noted above. Note that from the main, I call aliasNoPrompt().
Upvotes: 2
Views: 2104
Reputation: 651
Put the declaration to the top of your code. C requires code written in top to down order, meaning you should define AliasNode
before use it.
struct AliasNode {
struct AliasNode* next;
char* key;
char* value;
};
struct AliasNode* aliasHead = 0;
// ... other code here
Upvotes: 3
Reputation: 1070
Your struct declaration has to be above functions that use it.
struct AliasNode {
struct AliasNode* next;
char* key;
char* value;
};
struct AliasNode* aliasHead = 0;
void printAlias(struct AliasNode* alias) {
char* toPrint = alias->key;
printf(toPrint);
printf(": ");
toPrint = alias->value;
printf(toPrint);
printf("\n");
}
void checkAliases() {
if (aliasHead==0) {
printf("No aliases have been created.");
}
else {
struct AliasNode* current = aliasHead;
printf("Current Aliases: \n");
while (current!=0) {
printAlias(current);
current = current->next;
}
}
Upvotes: 2