Passing an array to linked list

This is a discussion on Passing an array to linked list within the C Programming forums, part of the General Programming Boards category; I'm attempting to pass an array to a linked list i've created and am having some difficulties. I've bolded the ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    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:
    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.
    Last edited by bar338; 04-08-2009 at 08:59 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    The prototype declares cord to be an array of ints but the parameter declaration of push_back()has it to be an array of pointers to int, as in.
    Code:
    void push_back(list *shapes, char *surface, int cord[]);   /* function prototype */
    void push_back(list *shapes, char *surface, int *cord[]){ ... }

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    int cord[] (used in the make_node prototype) is an array of integers.
    int *cord[] (used in push_back, including the "incompatible 3rd argument") is an array of int pointers.

    They are not the same.

    Your prototype and definition of "push_back" do not match because of the same issue.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    ahhhh i've been staring at that forever. Thanks. One last question. What is wrong with setting cord like this in make_node() in list.c

    tmp->cord = cord;

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You cannot assign arrays using the assignment operator. You need to copy them one member at a time.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Code:
      tmp->cord[0] = cord[0];
      tmp->cord[1] = cord[1];
      tmp->cord[2] = cord[2];
      tmp->cord[3] = cord[3];
      tmp->cord[4] = cord[4];
      tmp->cord[5] = cord[5];
      tmp->cord[6] = cord[6];
      tmp->cord[7] = cord[7];
    I know i can do it with a loop and i may. But fundamentally is it assigned like this?
    And am I using fscanf wrong? Right now i think everything in the text file is being assigned to surface. And each cord element has a value of 0.

    Do i need to seperate the string received from fscanf with a tokenizer or am i using fscanf wrong?

    This is what my text file looks like:
    Code:
    start,60,44,11,,11,33,55,53
    end,50,33,11,33,11,33,55,53
    surface,22,33,44,55,33,44,11,33
    surface,22,33,44,55,33,44,11,33
    surface,22,33,44,55,33,44,11,33
    surface,22,33,44,55,33,44,11,33
    surface,22,33,44,55,33,44,11,33
    surface,22,33,44,55,33,44,11,33
    surface,22,33,44,55,33,44,11,33
    surface,22,33,44,55,33,44,11,33
    surface,22,33,44,55,33,44,11,33
    surface,22,33,44,55,33,44,11,33

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    I believe you are needing to increment your cord elements. From the looks of it you will constantly overwrite them for each line read in. Also strange that your have 10 lines that are the same. If that is the case and to safe space why read all them in...is that really needed?

    But your fscanf statement look ok to me on the surface.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    it is just sample data. I will be creating text files later with more useful information. What do you mean by increment cord? I thought i was incrementing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM
  3. linked list from array problem
    By dcs in forum C++ Programming
    Replies: 5
    Last Post: 05-21-2004, 11:37 AM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21