Thread: Unix errors

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    Unix errors

    Hi Everyone,

    I trying to use the file ex3test.c to test the following program(ex3.c):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Step 1: define your struct here */
    typedef struct _Node {
      int data1, data2;
      struct _Node* next;
      struct _Node* next2;
    } Node;
    
    typedef struct _List {
      Node* front;
      Node* front2;
      Node* rear;
      Node* rear2;
    } List;
    
    static List list;
    
    /* Step 2: put code in the functions below to make the list work. */
    
    void clearList ()
    {
      while (list.front)
      {
        Node* tmp = list.front;
        list.front = list.front->next;
        free(tmp);
      }
    
      list.front = list.front2 = list.rear = list.rear2 = NULL;
    }
    
    void addData1(Node* newNode, int data1)
    {
      Node* prev = NULL;
      Node* iter = list.front;
    
      newNode->data1 = data1;
    
      while (iter && iter->data1 < data1)
      {
        prev = iter;
        iter = iter->next;
      }
      
      if (list.front == NULL)
      {
        list.front = list.rear = newNode;
        newNode->next = NULL;
      }
      else
        if (prev == NULL)
        {
          newNode->next = list.front;
          list.front = newNode;
        }
        else {
          newNode->next = iter;
          prev->next = newNode;
          if (prev == list.rear)
            list.rear = newNode;
        }
    }
    
    void addData2(Node* newNode, int data2)
    {
      Node* prev = NULL;
      Node* iter = list.front2;
    
      newNode->data2 = data2;
      while (iter && iter->data2 < data2)
      {
        prev = iter;
        iter = iter->next2;
      }
      
      if (list.front2 == NULL)
      {
        list.front2 = list.rear2 = newNode;
        newNode->next2 = NULL;
      }
      else
        if (prev == NULL)
        {
          newNode->next2 = list.front2;
          list.front2 = newNode;
        }
        else {
          newNode->next2 = iter;
          prev->next2 = newNode;
          if (prev == list.rear2)
            list.rear2 = newNode;
        }
    }
    
    void addToList (int data1, int data2)
    {
      Node* newNode = (Node*)malloc(sizeof(Node));
    
      addData1(newNode, data1);
      addData2(newNode, data2);
    }
    
    void printFirst ()
    {
      Node* iter = list.front;
    
      while (iter)
      {
        printf("(%d %d)\n", iter->data1, iter->data2);
        iter = iter->next;
      }
    }
    
    void printSecond ()
    {
      Node* iter = list.front2;
    
      while (iter)
      {
        printf("(%d %d)\n", iter->data1, iter->data2);
        iter = iter->next2;
      }
    }
    
    int findPos2(int data1, int data2)
    {
      int pos = 0;
      Node* iter = list.front2;
    
      while (iter && !(iter->data1 == data1 && iter->data2 == data2))
      {
        ++pos;
        iter = iter->next2;
      }
    
      return pos;
    }
    
    int disparity ()
    {
      int result = 0;
      Node* iter = list.front;
      int pos = 0;
    
      while (iter)
      {
        result += abs(findPos2(iter->data1, iter->data2) - pos);
    
        iter = iter->next;
        ++pos;
      }
    
      return result;
    }
    but those are the errors I am getting:

    [98] $ gcc -Wall -Wextra -o ex3 ex3.c ex3test.c uniform.c -lm
    ex3test.c:17: warning: type defaults to `int' in declaration of `discrete_uniform'
    ex3test.c: In function `addElements':
    ex3test.c:37: warning: implicit declaration of function `malloc'
    ex3test.c: In function `main':
    ex3test.c:85: warning: control reaches end of non-void function
    uniform.c: In function `zipf':
    uniform.c:91: warning: implicit declaration of function `malloc'


    how can I fix the problem?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    This line:

    Code:
    extern discrete_uniform (int, int);
    Should probably be:

    Code:
    extern int discrete_uniform (int, int);
    The other warnings can be corrected by #including <stdlib.h> in the corresponding files.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    ex3test.c and uniform.c need to have
    Code:
    #include <stdlib.h>
    ex3test.c needs this line:
    Code:
    /* This is needed for random generation */
    extern discrete_uniform (int, int);
    to match its definition in uniform.c:
    Code:
    extern long discrete_uniform (long, long);

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Include the header file where malloc() is declared ie stdlib.h. The extern declaration of discrete_uniform() has no return type and incorrect arguments. You have int's but they should be long's and it returns a long; and main() needs to return a value to its calling environment.
    Last edited by itCbitC; 09-23-2009 at 11:32 PM.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Thank everyone! I fixed the problems you guys pointed out, now this is the warning:

    [113] $ gcc -Wall -Wextra -o ex3 ex3.c ex3test.c uniform.c -lm
    ex3test.c: In function `main':
    ex3test.c:86: warning: control reaches end of non-void function
    Last edited by Samyx; 09-23-2009 at 11:53 PM.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Quote Originally Posted by itCbitC View Post
    Include the header file where malloc() is declared ie stdlib.h. The extern declaration of discrete_uniform() has no return type and incorrect arguments. You have int's but they should be long's and it returns a long; and main() needs to return a value to its calling environment.

    Can I say main returns 0?

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Samyx View Post
    Can I say main returns 0?
    Yes.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Now when I run the program I have this:

    ...
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ERROR in uniform.discrete_uniform(): a=100 b=99
    ...

    ???

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    I got it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Unix Sockets
    By prvindia in forum Linux Programming
    Replies: 5
    Last Post: 03-11-2003, 09:16 AM
  3. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM
  4. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM