Search code examples
cincludeassert

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


When I try and compile my code I get multiple errors concerning "assert", such as "unterminated list invoking macro assert" and "'assert' undeclared". This is despite me #include <assert.h> at the top of my code. Other files that use asserts which are in the same directory as this file compile and run with no errors, so I assume that it is the code which is the problem however I am unable to pinpoint what is causing it.

#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;
}

Here is the error:

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

Solution

  • The first error you're getting is at the end of the file at the last }. This means you have an unbalanced parenthesis or braces somewhere. The next error gives you a clue to where that's at:

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

    You're missing a ). You want:

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