0

I would like to exit the program when my argv[1] contains some character other than alphabet. Therefore, I stated:

for(int j = 0, p = strlen(argv[1]); j < p; j++)
    {
        if (false(isalpha(argv[1][j])))
        {
            printf("invalid character\n");
            return 1;
        }
    }

but it says error: called object type 'int' is not a function or function pointer. I don't understand what this means. This was from the CS50 week2's problem - substitution.

bearfist
  • 43
  • 4
  • 2
    What type did you expect and why? Are you looking for `if (!isalpha(...))`? – HolyBlackCat May 16 '21 at 14:27
  • 1
    You may want `!` instead of `false`. – MikeCAT May 16 '21 at 14:28
  • @EricPostpischil summarized it nicely in his reply! On a side (but irrelevant) note, `argv` always points to `nul`-terminated char-buffers, so there's no need to use `strlen()` at all in this case, and the code may be simplified to: `for (char *cp=argv[1]; *cp; cp++) { if (!isalpha(*cp)) {puts("invalid character"); return1;} }` – Harry K. May 16 '21 at 15:11

1 Answers1

5

There is no built-in function named false in C. Your expression false(isalpha(argv[1][j])) has the form false(x), so it looks like a function call.

The header <stdbool.h> defines a macro that expands to an integer constant zero. So false(x) is effectively 0(x). In this, 0 is an int constant. The compiler message “called object type 'int' is not a function or function pointer” is telling you that you cannot call this int 0 as if it were a function.

If you have some function in your program named false, it may have been effectively overridden by including <stdbool.h> (or possibly some other header file). If so, you should rename your function.

To test whether an expression is “false,” you can use the ! operator:

if (!isalpha(argv[1][j]))
{
    // Code here will be executed when isalpha(argv[1][j]) is false.
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312