Passing an array to linked list
I'm attempting to pass an array to a linked list i've created and am having some difficulties. I've bolded the problematic lines.
I am receiving these error messages:
Quote:
list.c: In function `make_node':
list.c:24: error: incompatible types in assignment
list.c: At top level:
list.c:32: error: conflicting types for 'push_back'
node.h:31: error: previous declaration of 'push_back' was here
list.c:32: error: conflicting types for 'push_back'
node.h:31: error: previous declaration of 'push_back' was here
list.c: In function `push_back':
list.c:40: warning: passing arg 3 of `make_node' from incompatible pointer type
I'm sure it has something to do with my less than complete understanding of pointers, arrays, and passing them to functions. I would gladly appreciate any help you could offer.
level.c
Code:
#include "node.h"
int main(void){
list *shapes = NULL;
shapes = (list *)malloc(sizeof(Node));
char surface[30];
int cord[8];
FILE *cfPtr;
if((cfPtr = fopen("file.txt", "r")) == NULL){
printf("File could not be opened\n");
}
else{
fscanf(cfPtr, "%s%d%d%d%d%d%d%d%d", surface, cord[0], cord[1], cord[2], cord[3], cord[4], cord[5], cord[6], cord[7]);
while(!feof(cfPtr)){
push_back(shapes, surface, cord);
fscanf(cfPtr, "%s%d%d%d%d%d%d%d%d", surface, cord[0], cord[1], cord[2], cord[3], cord[4], cord[5], cord[6], cord[7]);
}
fclose(cfPtr);
}
return 0;
}
node.h
Code:
#include <stdio.h>
#ifndef _NODE_H
#define _NODE_H
/* Struct to handle the linked list itself */
typedef struct node_tag{
char surface[30];
int cord[8];
struct node_tag *next;
struct node_tag *prev;
} Node;
typedef struct linked_list{
Node *head;
Node *tail;
} list;
Node *make_node(list *shapes, char *surface, int cord[]);
void push_back(list *shapes, char *surface, int cord[]);
void release(list *shapes) ;
void walk_list(list *shapes) ;
#endif
list.c
Code:
#include "node.h"
Node *make_node(list *shapes, char *surface, int cord[]){
Node *tmp = NULL;
tmp = (Node *)malloc(sizeof(Node)) ;
if (tmp == NULL) {
fprintf(stderr, "Error allocating memory in make_node().\n") ;
exit(1) ;
}
if (surface)
strncpy(tmp->surface, surface, sizeof(tmp->surface)) ;
tmp->cord = cord;
tmp->next = NULL ;
tmp->prev = NULL ;
return tmp ;
}
void push_back(list *shapes, char *surface, int *cord[]){
Node *tmp = NULL ;
if ((shapes->head == NULL && shapes->tail != NULL) || (shapes->head != NULL && shapes->tail == NULL)) {
fprintf(stderr, "Inconsistent list in call to push_front().\n") ;
exit(2) ;
}
tmp = make_node(shapes, surface, cord);
if (!tmp) {
fprintf(stderr, "Error building node in push_front().\n") ;
exit(3) ;
}
if (shapes->head == NULL && shapes->tail == NULL) {
shapes->head = tmp ;
shapes->tail = tmp ;
} else {
shapes->tail->next = tmp ;
tmp->prev = shapes->tail ;
shapes->tail = tmp ;
}
return ;
}
void release(list *shapes) {
while (shapes->head)
delete_node(shapes, shapes->tail) ;
return ;
}
void walk_list(list *shapes) {
Node *p = shapes->head ;
int count = 0 ;
if (shapes->head == NULL && shapes->tail == NULL) {
printf("======================\nEmpty List.\n=====================\n") ;
return ; /* not an error to try to print an empty list */
}
if ((shapes->head == NULL && shapes->tail != NULL) || (shapes->head != NULL && shapes->tail == NULL)) {
fprintf(stderr, "Error! Inconsistent list in call to walk_list()\n") ;
exit(4) ;
}
while (p) {
printf("Item[%d]: %s, %d, %d, %d, %d, %d, %d, %d, %d", count, p->surface, p->cord[0], p->cord[1], p->cord[2], p->cord[3], p->cord[4], p->cord[5], p->cord[6], p->cord[7]) ;
count++ ;
p = p->next ;
}
}
Thanks for your help, it is appreciated.