'assert' 未声明,尽管 assert.h 包含在文件中

'assert' undeclared, despite assert.h being included in the file

当我尝试编译我的代码时,我收到多个关于“断言”的错误,例如“未终止列表调用宏断言”和“'assert' 未声明”。尽管我在代码顶部使用了#include 。使用与此文件位于同一目录中的断言的其他文件编译并且 运行 没有错误,因此我认为这是代码问题但是我无法查明是什么导致了它。

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

#define NMBRS 1000000

int binIt (int k, int *a, int l, int r);
int binItRec (int k, int *a, int l, int r);
int interpSearch(int k, int * a, int l, int r);
int *parseArgs(int argc, char *argv[], int *n, int *search);

int main (int argc, char* argv[])
{
   int i, n, search;
   int *a;
   int (*p[3])(int k, int *a, int l, int r) = {binIt, interpSearch};

   a = parseArgs(argc, argv, &n, &search);

   srand(time(NULL));

   for (i = 0; i < n; i++) {
      a[i] = 2 * i;
   }
   for (i = 0; i < 1000000; i++) {
      if (search == 0) {
      assert((*p[search](a[rand() % n], a, 0, n - 1) >= 0);
      }
   }
   free(a);

   return 0;
}

int binIt (int k, int *a, int l, int r)
{
   int m;

   while (l <= r) {
      m = (l + r) / 2; 
      if (k == a[m]) { 
         return m;
      } else {
         if (k > a[m]) { 
            l = m + 1;
         } else {
            r = m - 1;
         }
      }
   }
   return -1;
}

int binItRec (int k, int *a, int l, int r)
{
   int m;

   if (l > r) {
      return -1;
   }

   m = (l + r) / 2;

   if (k == a[m]) {
      return m;
   } else {
      if (k > a[m]) {
         return binItRec(k, a, m + 1, r);
      } else {
         return binItRec(k, a, l, m - 1);
      }
   }
}

int interpSearch(int k, int * a, int l, int r)
{
   int m;
   double md;

   while (l <= r) {
      md = ((double)(k - a[l]) / (double)(a[r] - a[l]) * (double)(r - l)) + (double)(l); 
      m = 0.5 + md; 
      if (k == a[m]) {
         return m;
      } else {
         if (k > a[m])
            l = m + 1;
         else {
            r = m - 1;
         }
      }
   }
   return -1;
}

int *parseArgs(int argc, char *argv[], int *n, int *search)
{
   int *p;

   if (argc != 3) {
      fprintf(stderr, "I was expecting 2 arguments: %s <n> <searchtype: 0/1/2\n", argv[0]);
      exit(EXIT_SUCCESS);
   }
   if (sscanf(argv[1], "%d", n) != 1) {
      fprintf(stderr, "I was expecting 2 arguments: %s <n> <searchtype: 0/1/2\n", argv[0]);
      exit(EXIT_SUCCESS);
   }
   if (sscanf(argv[2], "%d", search) != 1) {
      fprintf(stderr, "I was expecting 2 arguments: %s <+ve n> <searchtype: 0/1/2\n", argv[0]);
      exit(EXIT_SUCCESS);
   }
   if ((*n < 1) || (*search < 0) || (*search > 2)) {
      fprintf(stderr, "I was expecting 2 arguments: %s <+ve n> <searchtype: 0/1/2\n", argv[0]);
      exit(EXIT_SUCCESS);
   }

   p = calloc(sizeof(int), *n);

   if (p == NULL) {
      fprintf(stderr, "Error: Failure to allocate memory\n");
      exit(EXIT_SUCCESS);
   }
   return p;
}

这里是错误:

gcc -Wall -Wextra -std=c90 -pedantic -lm -g3 -fsanitize=address -fsanitize=undefined    binarySearchUserv2.c   -o binarySearchUserv2
binarySearchUserv2.c: In function ‘main’:
binarySearchUserv2.c:126: error: unterminated argument list invoking macro "assert"
  126 | }
      |
binarySearchUserv2.c:29:7: error: ‘assert’ undeclared (first use in this function)
   29 |       assert((*p[search](a[rand() % n], a, 0, n - 1) >= 0);
      |       ^~~~~~
binarySearchUserv2.c:5:1: note: ‘assert’ is defined in header ‘<assert.h>’; did you forget to ‘#include <assert.h>’?

    4 | #include <assert.h>
  +++ |+#include <assert.h>
    5 |
binarySearchUserv2.c:29:7: note: each undeclared identifier is reported only once for each function it appears in
   29 |       assert((*p[search](a[rand() % n], a, 0, n - 1) >= 0);
      |       ^~~~~~
binarySearchUserv2.c:29:13: error: expected ‘;’ at end of input
   29 |       assert((*p[search](a[rand() % n], a, 0, n - 1) >= 0);
      |             ^
      |             ;
......
  126 | }
      |
binarySearchUserv2.c:29:7: error: expected declaration or statement at end of input
   29 |       assert((*p[search](a[rand() % n], a, 0, n - 1) >= 0);
      |       ^~~~~~
binarySearchUserv2.c:29:7: error: expected declaration or statement at end of input
binarySearchUserv2.c:29:7: error: expected declaration or statement at end of input
binarySearchUserv2.c:18:10: warning: unused variable ‘p’ [-Wunused-variable]
   18 |    int (*p[3])(int k, int *a, int l, int r) = {binIt, interpSearch};
      |          ^
binarySearchUserv2.c:126: warning: control reaches end of non-void function [-Wreturn-type]
  126 | }
      |
make: *** [<builtin>: binarySearchUserv2] Error 1

您遇到的第一个错误是在最后 } 文件的末尾。这意味着您在某处有不平衡的括号或大括号。下一个错误为您提供了错误位置的线索:

assert((*p[search](a[rand() % n], a, 0, n - 1) >= 0);

您缺少 )。你想要:

assert((*p[search])(a[rand() % n], a, 0, n - 1) >= 0);