C在linux终端上运行指针错误

时间:2016-02-18 03:06:43

标签: c linux

我是C的新手,不明白为什么我会收到此错误。该程序有效,但当我make arraycalc时,我收到了一个错误。这是代码

#include <stdio.h>
#include <stdlib.h>



void selection_sort (int *a, int n) ;
int count_even (int *a, int n);
void even_array(int *b, int *a, int n);
void printarray(int *a, int n,char *name, FILE *fPoint);
void odd_array(int *b, int *a, int n);

int main()
{
    // sets the pointer to the file
    FILE *fPointer, *fPointer2;
    fPointer= fopen("infile.txt", "r");
    fPointer2= fopen("outfile.txt", "w");

    //counts the number of lines
    int count =0;
    char c;
    for (c = getc(fPointer); c != EOF; c = getc(fPointer))
        if (c == '\n')
            count = count + 1;

    //sets the integer array up to the number of elements on the file
    int element[count];
    int line [256];  //size of line

    //resets pointer
    fPointer= fopen("infile.txt", "r");
    if (fPointer != NULL) //if pointer does not point to anything skip
    {
        int i =0;

        //goes through the file
        while (fgets(line,256, fPointer)!= NULL)
        {
            int num;
            fscanf(fPointer, "%d", &num);
            element[i]= num;
            // fputs(line,stdout);
            i++;
        }
        //Calls print and selection sort functions
        printarray(element,count,"Original Array", fPointer2);
        selection_sort(element, count);
        printarray(element,count,"Sorted Array", fPointer2);

        // counts the number of even elements using function call. Then subtracts it from the original array
        int countevenint= count_even(element,count);
        int countoddint= count-countevenint;

        // declare and sets size od oddarray
        int *evenarray[countevenint];

        //calls even array and copies even elements values into it
        even_array(evenarray,element,count);
        printarray(evenarray,countevenint,"Even Array", fPointer2);

        //declares and sets size of oddarray
        int *oddarray[countoddint];

        //calls odd array and copies odd elements values into it
        odd_array(oddarray,element,count);
        printarray(oddarray,countoddint,"Odd Array", fPointer2);


        fclose(fPointer);
        fclose(fPointer2);
    }
    else
    {

        perror("file.txt");
    }


    getchar();
    return 0;
}
//standard selection sort.
void selection_sort (int *a, int n)
{
    int i, j, m, t;
    // i is used to go through the list of size n. j and m to compare and t as temporary value
    for (i = 0; i < n; i++)
    {
        for (j = i, m = i; j < n; j++)
        {
            if (a[j] < a[m])// if element in j is smaller than element at m then pointer at m becomes j
            {
                m = j;
            }
        }
        //standard sorting using temp values
        t = a[i];
        a[i] = a[m];
        a[m] = t;
    }
}
//accepts an array, size, and Header name and filepointer to print on terminal and in filed called "outfile"
void printarray(int *a, int n,char *name, FILE *fPoint)
{
    printf("\n%s\n",name);
            fprintf(fPoint,"\n%s\n",name);
    int i;

    //goes through the list and prints every element in the array
    for (i = 0; i < n; i++)
    {
        fprintf(fPoint," %d", a[i]);
        printf(" %d", a[i]);

    }
    fprintf(fPoint," \n");
    printf(" \n");
}

//counts how man even numbers in the original array. Accepts an array and size
int count_even (int *a, int n)
{
    int count=0;
    int i;
    for (i = 0; i < n; i++)
    {
        if (a[i]%2==0)
        {
            count++;
        }
    }
    return count;

}

//Gets the even array b and the initial array a and a size n. Seperates the even numbers into b
void even_array(int *b, int *a, int n)
{

    int i;
    int j =0;
    for (i = 0; i < n; i++)
    {
        if (a[i]%2==0)
        {
            b[j]=a[i];
            j++;

        }

    }
}

//Same as even array, but for odd numbers
void odd_array(int *b, int *a, int n)
{

    int i;
    int j =0;
    for (i = 0; i < n; i++)
    {
        if (a[i]%2==0)
        {

        }
        else
        {
            b[j]=a[i];
            j++;

        }

    }
}

它从包含的文件“infile.txt”中提取 排列 12 3 4 65 23 21 4 2 10 13

和Makefile有 所有:     g ++ arraycalc.c -o arraycalc 编译:

为什么指针返回错误的想法?

$ make arraycalc
cc     arraycalc.c   -o arraycalc
arraycalc.c: In function ‘main’:

arraycalc.c:40:22: warning: passing argument 1 of ‘fgets’ from incompatible pointer type
         while (fgets(line,256, fPointer)!= NULL)
                      ^
In file included from arraycalc.c:1:0:
/usr/include/stdio.h:622:14: note: expected ‘char * __restrict__’ but argument is of type ‘int *’
 extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
              ^
arraycalc.c:61:20: warning: passing argument 1 of ‘even_array’ from incompatible pointer type
         even_array(evenarray,element,count);
                    ^
arraycalc.c:11:6: note: expected ‘int *’ but argument is of type ‘int **’
 void even_array(int *b, int *a, int n);
      ^
arraycalc.c:62:20: warning: passing argument 1 of ‘printarray’ from incompatible pointer type
         printarray(evenarray,countevenint,"Even Array", fPointer2);
                    ^
arraycalc.c:12:6: note: expected ‘int *’ but argument is of type ‘int **’
 void printarray(int *a, int n,char *name, FILE *fPoint);
      ^
arraycalc.c:68:19: warning: passing argument 1 of ‘odd_array’ from incompatible pointer type
         odd_array(oddarray,element,count);
                   ^
arraycalc.c:13:6: note: expected ‘int *’ but argument is of type ‘int **’
 void odd_array(int *b, int *a, int n);
      ^
arraycalc.c:69:20: warning: passing argument 1 of ‘printarray’ from incompatible pointer type
         printarray(oddarray,countoddint,"Odd Array", fPointer2);
                    ^
arraycalc.c:12:6: note: expected ‘int *’ but argument is of type ‘int **’
 void printarray(int *a, int n,char *name, FILE *fPoint);

1 个答案:

答案 0 :(得分:1)

fgets语法char * fgets(char * str,int n,FILE * stream)

但是你传递的是整数数组int line [256];

相关问题