Expression preceding parentheses of apparent call must have (pointer-to-) function type

Explanation:

The error message “expression preceding parentheses of apparent call must have (pointer-to-) function type” occurs when we try to call a function but the expression preceding the parentheses is not a function or a function pointer.

Here’s an example that will cause this error:

    
#include <stdio.h>

int add(int a, int b) {
  return a + b;
}

int main() {
  int result = add(2, 3); // Error: add is not a function pointer
  printf("Result: %d\n", result);
  return 0;
}
    
  

In this example, we have a function called “add” that takes two parameters and returns their sum. In the main function, we try to call the “add” function with arguments 2 and 3. However, the error occurs because “add” is not a function pointer.

In order to fix this error, we need to use the function name without parentheses as it represents a function pointer. Here’s the corrected example:

    
#include <stdio.h>

int add(int a, int b) {
  return a + b;
}

int main() {
  int (*funcPtr)(int, int) = &add; // Function pointer to add function
  int result = funcPtr(2, 3); // Call the function using function pointer
  printf("Result: %d\n", result);
  return 0;
}
    
  

In this corrected example, we declare a function pointer named “funcPtr” that can point to a function taking two integer parameters and returning an integer. We initialize this function pointer to point to the “add” function using the “&” operator. Then, we can call the function using the function pointer “funcPtr”.

Read more interesting post

Leave a comment