У меня есть фрагмент кода, который проверяет, определен ли макрос уже, а если нет, то выделяет память для нового макроса и добавляет его в текущий список.Если он уже определен, он просто меняет тело макроса и сохраняет то же имя.
static struct macro *macro_lookup(char *name){
struct macro * temp = ¯o_list;
while(temp->next != NULL){
if(strcmp(temp->macro_name,name) == 0){
return temp;
}
}
return NULL;
}
void macro_set(char *name, char *body){
//Need to check to see if a macro is already set for the name if so just change the body
struct macro * p = macro_lookup(name); //Will return NULL if macro is not in the list. This line gives me the error of segmentation fault 11, if I comment it out the program works.
//Need to make a new macro and add it to the list
if(p == NULL){
//Make a new macro
struct macro * new_macro = (struct macro *) Malloc(sizeof(struct macro)); //Malloc is my version of malloc, it works just fine.
if(new_macro == NULL){
fprintf(stderr,"Error while allocating space for the new marco.\n");
exit(EXIT_FAILURE);
}
new_macro->macro_name = name;
new_macro->macro_body = body;
//Create a pointer to the list and traverse it until the end and put the new macro there
struct macro * temp = ¯o_list;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = new_macro;
}
//The macro already exists and p is pointing to it
else{
//Just change the body of the macro
p->macro_body = body;
}
}
Я не знаю, почему строка ошибки, приведенная выше, создает мне проблему, я могу статически установить p вnull и протестируйте его, и он работает нормально, но когда я использую функцию macro_lookup, он получает ошибку сегмента.