c - 传递“getGradesFromFile”的参数 1 从指针生成整数,无需强制转换

标签 c

我正在编写一个程序来读取记录文件,将一些字段提取到 VLA 中,使用插入排序对它们进行排序,使用二分搜索搜索用户指定的目标,然后打印结果。

但是我不断收到这些错误:

search.c: In function âmainâ:
search.c:57: warning: passing argument 1 of âgetGradesFromFileâ makes integer from pointer without a cast
search.c:19: note: expected âintâ but argument is of type âchar *â
search.c:57: warning: passing argument 2 of âgetGradesFromFileâ makes pointer from integer without a cast
search.c:19: note: expected âchar *â but argument is of type âintâ
search.c:57: warning: passing argument 3 of âgetGradesFromFileâ from incompatible pointer type
search.c:19: note: expected âint *â but argument is of type âchar *â
search.c:57: warning: passing argument 4 of âgetGradesFromFileâ makes integer from pointer without a cast
search.c:19: note: expected âintâ but argument is of type âint *â
search.c:57: warning: passing argument 7 of âgetGradesFromFileâ makes pointer from integer without a cast
search.c:19: note: expected âchar *â but argument is of type âintâ
search.c:57: error: too many arguments to function âgetGradesFromFileâ
search.c:59: warning: passing argument 1 of âprintArrayâ makes integer from pointer without a cast
search.c:38: note: expected âintâ but argument is of type âchar *â
search.c:59: warning: passing argument 2 of âprintArrayâ makes pointer from integer without a cast
search.c:38: note: expected âchar *â but argument is of type âintâ
search.c:62: error: expected expression before âintâ
search.c:62: error: too few arguments to function âinsertionSortâ
search.c:67: warning: comparison between pointer and integer
search.c:68: warning: comparison between pointer and integer
search.c:68: warning: comparison between pointer and integer
search.c:69: warning: passing argument 3 of âbinarySearchâ makes integer from pointer without a cast
search.c:33: note: expected âintâ but argument is of type âint (*)[60]â
search.c:69: error: too few arguments to function âbinarySearchâ
search.c:73: error: expected â(â before â{â token

感谢您对这些错误的任何帮助!!

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




void getGradesFromFile(int size,
                            char line[],
                            int studentI_D[],
                            int test1,
                            int test2,
                            int test3,
                            char grades[]);



void insertionSort(char list[],
                   int last);


void binarySearch(int size,
                int studentID[],
                int target,
                 char* locn);

void printArray(int size,
                char list[*],
                int studentI_D[*],
                int test1,
                int test2,
                int test3,
                char grades[*]);

int main(void)
{
  char grades[60];
  char str[60];
  int size = sizeof(str) / sizeof(str[60]);;
  int studentID [60];
  char letter;
  int exam1, exam2, exam3;


  getGradesFromFile("grades.csv", size, str, studentID, exam1, exam2, exam3, grades);// function call
  printf("The original numbers are:");
  printArray(str, size, studentID, exam1, exam2, exam3, grades);
  printf("\n\n");
  printf("The sorted numbers are:");
  insertionSort(int size, studentID, grades);// inscertion sort function call
  printf("\n\n");
  printf("Enter student ID and -1 to quit");
  scanf("%d", studentID); 

  while (studentID != -1){
  if (studentID > 99999 && studentID < 1000000){
  binarySearch(size, studentID, &studentID);


}
  else if {
 printf ("Error and enter again [100000, 999999] and -1 to quit");
}
exit;
  }



  return 0;
}  // end of main

void getGradesFromFile(int size,
                            char line[],
                            int studentI_D[],
                            int test1,
                            int test2,
                            int test3,
                            char grades[])
{               
  FILE* pData;
  int i = 0;
  if ((pData == NULL) {// opens file grades.csv
    fprintf(stderr, "Error opening file %s.\n");// error handling statement
    exit(1);
  }


  while (fgets(line,sizeof(line), pData) != NULL){
sscanf(line, "%25s, %d, %d, %d, %c", studentID[i], &test1, &test2, &test3,
grades[i]);
i++;
} 
  }

  if fclose(pData) {
    fprintf(stderr, "Error closing file %s.\n", filename);// close file
    exit(2);
  }
}

void insertionSort(int length, int studentID[], char grades[])
{
        int i, key, key1, j;
   for (i = 1; i < n; i++)
   {
       key = studentID[i];
       key1= grades[i];
       j = i-1;

       while (j >= 0 && studentID[j] > key)
       {
           studentID[j+1] = studentID[j];
           grades[j+1] = grades[j];
           j = j-1;
       }
       studentID[j+1] = key;
       grades[i+j] = key1;
   }

}

void binarySearch(int size, int student[], int target,  char *locn)
{
     int  first = 0;
     int last = size - 1; 
     int mid;
     bool found = false;  

        while (first <= last) {
                mid = (first + last) / 2;
        if (target > list[mid]) {
                first = mid + 1;
        } else if (target < list[mid]) { 
        last = mid - 1;
        } else {   
        *locn = mid;
        found = true;
        break;
        }
        }
return found;
} 

void printArray(int size,
                char A[size],
                int studentI_D[size],
                int test1,
                int test2,
                int test3,
                char grades[size])
{
  for (int i=0; i<size; i+=4) {// prints the array in four lines
    printf("\n");
    for (int j=0; j<4; j++) {
    printf("%10.2f ", A[i+j]);
  }
} 
}

void flushScanf(void)
        {
  char c;
  while((c = getchar() != '\n' && c != EOF)
        ;
}

最佳答案

void getGradesFromFile(int size,
                        char line[],
                        int studentI_D[],
                        int test1,
                        int test2,
                        int test3,
                        char grades[]);

sizeint 类型,但您传递的是 "grades.csv" 这是一个字符串,因此它的地址将被 paased,因此它变成指针。

line[] 是一个数组,因此是一个指针,您正在传递 int 类型的 size

studentI_D[]int 类型的数组。您正在传递 char 类型的 str 等等......

您的函数声明和定义有 7 个参数,但您传递了 8 个参数,因此导致错误。

你需要自己检查一下休息情况。这些都是愚蠢的错误。

关于c - 传递“getGradesFromFile”的参数 1 从指针生成整数,无需强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646638/

相关文章:

c - 在c中使用指针进行矩阵乘法

c - send() 什么时候返回 EWOULDBLOCK?

c++ - #if/#endif 预处理指令

c - 用值加载堆栈指针是特权指令吗?

C语言: why c language has 'class' ?

c - 静态库和动态库有什么区别

C编程: testing oversized shift

c - 这个指针有什么问题?

c++ - 将函数内的大变量声明为 `static` 在性能上有什么不同吗?

c - 类型 "char (*)[16]"的参数与类型 "const char *"的参数不兼容