Thread: Passing argument 1 makes integer from pointer without a cast + few more pointer error

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    Passing argument 1 makes integer from pointer without a cast + few more pointer error

    I am having some errors with pointers and passing arguments.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    #define MAX_FILE_LENGTH 20
    
    
    typedef struct node_{
            int value;
            struct node_* next;
    }Node;
    
    
    typedef Node* List;
    
    
    int create_list(List**,FILE*);
    void print_list(List*, int);
    void free_list(List*, int);
    int list_sum(Node*);
    void insertion_sort(List*, int);
    Node* new_node(int value);
    
    
    /* Main will read in an input file from the command line, 
     * if the file fails to open the program should prompt the user for a new file, 
     * at no point should the program quit. If the program reads in a new file name, 
     * it can be up to size MAX_FILE_LENGTH. 
     * From there the program should read in the file, print it out, sort it, 
     * print it out again and then free it. */
    
    int main (int argc, char* argv[]){
    
    
        if(argc != 2){
                printf("Incorrect number of command line arguments\n");
                return 1;
        }
    
    
        FILE* fp = fopen(argv[0], "r");
    
    
        while(fp == NULL){
                char file[MAX_FILE_LENGTH];
                printf("Unable to open file, enter a new file name: ");
                scanf("%d", file);
                fp = fopen(file, "r");
        }
    
    
    
        List *array;
        int length = create_list(&array, fp);
    
    
        printf("Pre Sort\n");
        print_list(array, length);
    
    
        insertion_sort(array,length);
        printf("\nPost Sort\n");
      print_list(array, length);
    
    
        return 0;
    }
    
    
    /*This function takes in a pointer to a list and a file pointer. 
     * The first line of the input file is the length of the array to be created.
     * Each subsequent line is composed of two numbers, an index and a value. 
     * The index is the index of the linked list where a node with the value of value should be inserted. 
     * So for example, if index = 0 and value = 3, and before the insertion array[0] = 1 -> 2 -> NULL, 
     * after the insertion array[0] = 3 -> 1 -> 2 -> NULL. This function returns the length of the array.*/
    
        int create_list(List** array,FILE* fp){
            int length, i, index, value;
    
    
            fscanf(fp, "%d", &length);
    
    
            array = malloc(sizeof(Node));
    
    
            for(i = 0; i < length; i++)
                    (*array)[i] = NULL;
    
    
            while(1){
                    fscanf(fp, "%d %d", &index, &value);
    
    
                    Node* node = new_node(&value);
    
    
                    node->next = array[index];
    
    
                    array[index] = node;
            }
    
    
            return length;
    }
    
    
    /*Creates a new node of type Node, sets node->value = value and returns it. */
    
        Node* new_node(int value){
            Node* node;
            node->value = value;
            node->next = NULL;
    
    
            return node;
    }
    
    
    /*Takes in the head to a single linked list and returns the sum of the values of each of its nodes. 
     * Ex: Node* head = 1 -> 2 -> 3 -> NULL then list_sum(head) = 6.*/
    
        int list_sum(Node* head){
    
    
            return head->value + list_sum(head->next);
    }
    
    
    /*For each index in the array, print the index, the linked list it points to,
     * and the sum of its nodes. See the sample output for an example.*/
    
        void print_list(List* array, int length){
            int i;
    
    
            for(i = 0; i < length; i++){
                    Node *curr = array[i];
    
    
                    printf(" -\n|%d| ", i);
    
    
                    printf("%d ->", curr->value);
    
    
                    curr = curr->next;
    
    
                    printf("NULL = %d\n -\n", list_sum(array[i]));
            }
    
    
            /*3)return curr;*/
    }
    
    
    /*Sorts the array using insertion sort in ascending order by the sums of each linked list. */
    
        void insertion_sort(List* array, int length){
            int* sum = malloc(sizeof(int) * length);
            int i, j, value;
            Node* node;
    
    
            for(i = 1; i < length; i++){
                    value = sum[i];
                    node = array[i];
                    for(j = i; j > 0 && value < sum[j + 1]; j++)
                    {
                            sum[j] = sum[j - 1];
                            array[j] = array[j - 1];
                    }
            }
    
    
            printf("\n");
            free(sum);
    }
    /*Free all allocated memory.*/
    
        /*4)void free_list(List array, int length){*/
        void free_list(List* array, int length){
            int i;
            Node *curr;
            while(curr != NULL){
                    Node* prev = curr;
                    curr = curr->next;
                    free(curr);
            }
    }

    Here are the few errors that I get when I compile my code.
    homework2.c: In function ‘create_list’:
    homework2.c:77: warning: passing argument 1 of ‘new_node’ makes integer from pointer without a cast
    homework2.c:20: note: expected ‘int’ but argument is of type ‘int *’
    homework2.c:79: warning: assignment from incompatible pointer type
    homework2.c:81: warning: assignment from incompatible pointer type

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by LegitDabber View Post
    Here are the few errors that I get when I compile my code.
    Your code? Why does your code look virtually identical to your classmate's? You know you can be expelled for such blatant plagiarism and academic dishonesty. Besides, we don't tolerate it here on this board, so you wont get any help until you actually post your own code that you actually wrote yourself.
    Code:
    $ diff -u --ignore-blank-lines original.c cheater.c 
    --- original.c	2014-03-26 15:44:01.976231404 -0700
    +++ cheater.c	2014-03-26 15:43:37.405946752 -0700
    @@ -7,7 +7,6 @@
     
     typedef struct node_{
         int value;
    -    /*1)    struct node_ next;*/
         struct node_* next;
     }Node;
     
    @@ -49,9 +49,9 @@
         }
     
     
    -    /*2)List array;*/
    -    List **array;
    -    int length = create_list(array, fp);
    +
    +    List *array;
    +    int length = create_list(&array, fp);
     
     
         printf("Pre Sort\n");
    @@ -91,7 +92,7 @@
             fscanf(fp, "%d %d", &index, &value);
     
     
    -        Node* node = new_node(value);
    +        Node* node = new_node(&value);
     
     
             node->next = array[index];

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Could be a coincidence...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Another possibility is that this code was written by the tutor and deliberately broken before giving to the students as a "fix the errors" homework.
    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. Replies: 5
    Last Post: 07-05-2012, 11:12 AM
  2. Replies: 7
    Last Post: 03-07-2012, 07:14 PM
  3. Replies: 2
    Last Post: 11-20-2011, 11:36 AM
  4. Replies: 8
    Last Post: 08-28-2009, 07:49 AM
  5. Replies: 4
    Last Post: 01-27-2009, 02:33 PM

Tags for this Thread