I am confused why I get the error message "src/parser.c:13:1: error: unknown type name 'declared_idents'; did you mean 'declared_t'?" when I declare the struct declared_idents prior to defining the members of the struct.
However, when I move the declared_idents.declared_list and declared_idents.declared_list_length definitions inside the function "is_declared" my code compiles fine. Why is this happening? My code is below:
Parser.c
#include "../include/parser.h"
#include "../include/lexer.h"
operators_t operators[] = {
{"=", 201}, {"+", 202}, {"-", 203}, {"*", 204}, {"/", 205},
{"==", 206}, {"!=", 207}, {"<", 208}, {"<=", 209}, {">", 210}, {">=", 211},
};
int operators_length = sizeof(operators)/sizeof(operators[0]);
declared_t declared_idents;
char declare_container[TOKEN_BUFSIZE][BUFSIZE];
declared_idents.declared_list = &declare_container;
declared_idents.declared_list_length = 5;
int is_declared(char *identifier) {
int i = 0;
for (i; i < declared_idents.declared_list_length; i++) {
if (strcmp(identifier, declared_idents.declared_list[i]) == 0) {
return 1;
}
}
declared_idents.declared_list[i] = *identifier;
(declared_idents.declared_list_length)++;
return 0;
}
Moving the definitions at the top of the file into the function "int is_declare(..) compiles with no errors:
int is_declared(char *identifier) {
declared_idents.declared_list = &declare_container;
declared_idents.declared_list_length = 5;
}
Parser.h
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define BUFSIZE 1024
#define TOKEN_BUFSIZE 64
typedef struct {
char **declared_list;
int declared_list_length;
} declared_t;
int is_declared(char *identifier);
When I read the logs from the compiler, I see that it reads the header file "src/../include/parser.h:15:3: note: 'declared_t' declared here } declared_t;" after flagging the error.