Thread: making a node in linked lists

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    56

    making a node in linked lists

    okay i can make nodes jsut fine using numbers but i seem to be getting problems using toher data such as strings.the rest of the program works fine just this part crashes


    Code:
    typedef struct node{
                        char word[20];
                      
                        struct node *next;
                        }Node,*Nodeptr;
    
    
    .....
    Nodeptr top=NULL;
      fscanf(in,"%s",&wod);
      strcpy(top,makenode(wod));
    
    
    ....
    }
    
    ///function to make node
    Nodeptr makenode(char string){
            Nodeptr str;
            str=(Nodeptr)malloc(sizeof(Node));
            strcpy(str->word,string);
            str->next=NULL;
            return str;
            }

    these are the compiler errors

    26 C:\Users\killasan\Documents\comp porgrams\ass2.c [Warning] passing arg 1 of `makenode' makes integer from pointer without a cast


    26 C:\Users\killasan\Documents\comp porgrams\ass2.c [Warning] passing arg 1 of `strcpy' from incompatible pointer type

    in function makenode 35 C:\Users\killasan\Documents\comp porgrams\ass2.c [Warning] passing arg 2 of `strcpy' makes pointer from integer without a cast

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    okay ive gotten some errors worked out so far but my programs crashes but no errors in the compiler.this is my entire program.nneeds to read from a file though.please tell me what my errors can be
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    FILE*in;
    
    typedef struct node{
                        char word[20];
                        int count;
                        struct node *next;
                        }Node,*Nodeptr;
                        
     Nodeptr addinplace(Nodeptr ,char[] ); 
     Nodeptr makenode(char[] );                  
                        
    int main(){
        in=fopen("shims.txt","r");
        if(in==NULL){
                     printf("Filenot found!! \n");
                     system("pause");
                     exit(1);
                     }
        char wod[20];
        Nodeptr start=NULL;
        fscanf(in,"%s",&wod); 
        while(strcmp(wod,'\0')!=0){
                                   start=addinplace(start,wod);
                                   fscanf(in,"%s",&wod);
                                   }
        
        fclose(in);                    
        system("pause");
        return 0;
    }
    //making a node to hold the word
    Nodeptr makenode(char string[]){
          
            Nodeptr str=(Nodeptr)malloc(sizeof(Node));
            strcpy(str->word,string);
            str->next=NULL;
            return str;
            }
     //adds words inorder into th list       
     
    Nodeptr addinplace(Nodeptr top,char string[]){
            Nodeptr str,curr,prev,makenode(char[]);
            
            str=makenode(string);
            prev=NULL;
            curr=top;
    while(curr!=NULL && strcmp(curr->word,string)<0){
            prev=curr;
            curr=curr->next;
            }
    if(prev==NULL){
                   str->next=top;
                   return str;
                   }
             str->next=curr;
             prev->next=str;
             return top;
             }

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this is C - and should be moved to the correct forum

    fscanf(in,"%s",&wod);

    wod is array - so it will be converted to the pointer automatically, no need for & here
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM