Reputation: 49
"I'm trying to implement a Linked List that will later make uses of a lexicographical sort function, but I keep getting an error concerning conflicting types in listnode.
I don't know what the cause of this issue is. Previously I had issues trying to malloc using sizeof and I found that the solution was declaring next as listnode* pointer. It solved that issue, but it still has conflicting types. When I try to compile I get this error message:
ls.c:18:3: error: conflicting types for 'listnode'
} listnode:
ls.c:12:14: note previous declaration of 'listnode' was here
typedef node listnode;
My code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
int strcmp(const char *str1, const char *str2);
//This chunk of code is dedicated to the formation and functions of Linked list
//which will be used to store and sort file names.
typedef struct node node;
typedef node listnode;
#define EMPTY NULL;
typedef struct listnode{
struct listnode* next;
char* string;
} listnode;
struct listnode* head;
//This function will print the entire list
void printlist(listnode* head){
struct listnode* current = head;
while(current != NULL){
printf("%s \n", current->string);
current = current->next;
}
}
//This function creates a new node
listnode* newNode(char* str, listnode* node){
listnode* new = (listnode*)malloc(sizeof(listnode*));
if(new == NULL){
printf("Error creating new listnode.\n");
exit(0);
}
new->string = str;
new->next = node;
return new;
}
//This function will append a new node onto the list
listnode* list_append(listnode* head, char* str){
if (head == NULL){
listnode* new = newNode(str, head);
head = new;
return head;
}
else{
listnode* current = head;
while(current-> next != NULL){
current = current->next;
}
listnode* new = newNode(str,NULL);
current -> next = new;
}
return head;
}
//This function earses the list freeing up space
void list_free(listnode* head){
listnode* current;
listnode* temp;
if(head != NULL){
current = head->next;
if(head !=NULL){
current = head -> next;
head ->next = NULL;
while(current != NULL){
temp = current -> next;
free(current);
current = temp;
}
}
}
free(head);
}
//This is the end of the linked list code
int main(int argc, char **argv){
char *current_dir = NULL;
DIR *direct_ptr = NULL;
struct dirent *dir_ptr = NULL;
unsigned int fileNum = 0;
int c;
listnode* head = NULL;
current_dir = getenv("PWD");
if(NULL == current_dir){
printf("\n Error: Couldn't grab current directory.\n");
return -1;
}
direct_ptr = opendir((const char*)current_dir);
if(NULL == direct_ptr){
printf("\n Error: couldn't open current directory\n");
return -1;
}
if(argc == 1){
for(fileNum=0; NULL != (dir_ptr = readdir(direct_ptr)); fileNum++){
if(dir_ptr->d_name[0] != '.'){
head = list_append(head, dir_ptr->d_name);
}
}
}
else{
if(strcmp(argv[1], "-a") || strcmp(argv[1], "[-a]")){
for(fileNum=0; NULL != (dir_ptr = readdir(direct_ptr)); fileNum++){
head = list_append(head, dir_ptr->d_name);
}
}
else{
printf("\n Unrecognized argument in command line.\n");
return -1;
}
}
return 0;
}
Upvotes: 0
Views: 488
Reputation: 781878
You have two definitions of listnode
:
typedef node listnode;
and
typedef struct listnode{
struct listnode* next;
char* string;
} listnode;
The first one is unnecessary, and won't work, because it uses node
, which is defined as
typedef struct node node;
but you never defined struct node
anywhere.
Upvotes: 0
Reputation: 224437
You do in fact have conflicting definitions for listnode
. You first define the type here:
typedef struct node node;
typedef node listnode;
Then define it again here:
typedef struct listnode{
struct listnode* next;
char* string;
} listnode;
In one case it's an alias for struct node
and in the other it's an alias for struct listnode
.
Get rid of the first one as well as typedef struct node node;
Upvotes: 0