Thread: Linked list w/ structure

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    Linked list w/ structure

    http://rafb.net/paste/results/tJoB4z75.html

    After executing the following code I receive the errors:
    a.c: In function `insert':
    a.c:59: error: incompatible types in assignment


    I have no problem when first_name in the structure is an int. I can easily assign a default value in the insert function. But I would like to allow full strings to be sent to the field from the insert function....Any ideas?

    The code is:

    Code:
    /*Program to illustrate construction of linked list
      Written by Ron
      April 2006
    
      Language: C (gcc target)
    */
    
    
    
    #include <stdio.h>
    #include <stdlib.h>   /*malloc is in stdlib, malloc.h not required*/
    
    typedef struct member
      {int number; int id_number; char first_name[30];
       struct member *next;
      }RECORD;
    
    RECORD* insert(RECORD *it, int value); /*Prototype for insert*/
    void traverse_and_print(RECORD *head);
    
    int max = 0;
    int main(void)
      {
        int i, result, count;
        RECORD *head, *p;
        head=NULL;
        result=1;
        for (i=1; i<=5; i++)
           head=insert(head,i);
    /* DRAW PICTURE OF LINKED LIST AT THIS POINT OF PROGRAM*/
        traverse_and_print(head);
        return 0;
      }
    
    
    RECORD* insert(RECORD *it, int value)
    /*Function to insert a record into a linked list
      Written by Ron
      April 2006
    
      Language: C (Borland 5.01)
    */
    {
       RECORD *cur, *q;
       int i; char first_name[30]; char last_name[30];
       //printf("Enter an employee number\n");
       //scanf( "%d", &i );
       //printf("Enter first name\n");
       //scanf( "%s", &first_name );
       //printf("Enter last name\n");
       //scanf( "%s", &last_name );
    
    
    
    //   printf("Entering insert, value is %d\n", value);
       cur=(RECORD *)malloc(sizeof(RECORD));
       cur->number=value;
       cur->id_number=i;
       cur->first_name="First Name";
    
       cur->next=NULL;
       if (it==NULL)
          it=cur;
       else
          {q=it;
           while(q->next!=NULL)
             q=q->next;
           q->next=cur;
          }
       return(it);
    }
    
    void traverse_and_print(RECORD *head)
    /*Function to traverse linked list, print values in records, and
      mutiply them*/
    
    { RECORD *p;
      int result;
      result=1;
      p=head;
      while (p!=NULL)
         {result=p->number;
          //printf("num is %d", p->id_number);
          if(result > max){max = result;}
          p=p->next;
         }
      printf("MAX IS %d", max);
    
      while (p!=NULL)
         {result=p->number;
          //printf("num is %d", p->id_number);
          if(result > max){max = result;}
          p=p->next;
         }
    
    
      return;
    }
    Last edited by 5rjK; 12-11-2006 at 03:27 AM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    strcpy(cur->first_name, "First Name");

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    strncpy is better
    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

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    I need to find the structure with the smallest id_number. I figured it to find the largest one I could do:

    Code:
       strcpy(cur->first_name, first_name);
       strcpy(cur->last_name, last_name);
    
       if(i > max){max = i;post_id=value;}

    But I can't figure out how to find the minimum since the function is continously called.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> strncpy is better

    Dirr -- strcpy is faster!

    >> I need to find the structure with the smallest id_number.

    I do not understand what you mean. The member of your linked list which has the smallest ID? How does the code you posted illustrate that (finding the max one I guess in the case you posted) and similarly what is post_id.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should initialize your starting min vlue to something big enough to be bigger than any possible value in the list, then your approach will work also for finding min

    (you can use something like INT_MAX for example)
    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

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> you should initialize your starting min vlue to something big enough
    >> to be bigger than any possible value in the list, then your approach
    >> will work also for finding min
    >>
    >> (you can use something like INT_MAX for example)

    Or you can say (after checking for the case of an empty list) that the min is equal to the first element of the list and then progress through the list reassigning the min if you find a smaller one.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by vart
    strncpy is better
    And potentially more prone to misuse.
    http://c-faq.com/lib/strncpy.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    When I look for a min value I like to go opposite of what I do in max...a generic code would be:
    Code:
    minimum = current_i_value;
    max = (some ridiculously huge number, within reason);
    
    for(i = max; i > 0; i--)
    {
        if(current_i_value < minimum)
              minimum = current_i_value;
    }
    
    return minimum;
    It seems kinda weird but after you use it twice it makes sense and you'll never screw it up with max due to the decrement operator. But I'm new at this so don't listen to me :-).
    Last edited by verbity; 12-11-2006 at 09:59 PM.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Dave_Sinkula
    And potentially more prone to misuse.
    http://c-faq.com/lib/strncpy.html
    any of suggested alternatives are good for me - all prevent from buffer overrun. That strcpy does not.
    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

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Write a 2-line function which wraps around strncpy() and always appends a \0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM