-3

These are some lines of an example code in a C tutorial book that I can't understand.

I have already learned operators, program structure, variables, I/O, decision making & loops, array, strings ..., functions and right now I am at chapter 6 pointers.

void check(char *a,char *b, int (*cmp)(const char *,char *)); // this one
//int cmp(char * , char *);
int main()
{
    char s1[80],s2[80];
    int (*p)(const char *,const char *); //  and this one



if(!(cmp)(a,b)) //and this

(this code is not complete)

Is this normal? Should I use a different source?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • They are two pointers to functions. The syntax is a bit weird, but what's required if you need a pointer to a function. The notation `if (!(cmp)(a, b))` is an invocation of the function identified by the function pointer `cmp`. The parentheses around `(cmp)` are not actually needed — they would be if it was written `if (!(*cmp)(a, b))`, which would also work. – Jonathan Leffler Jul 04 '19 at 04:41

1 Answers1

2
void check(char *a,char *b, int (*cmp)(const char *,char *)); // this one

This is a function which takes 3 parameters,

  1. a character pointer
  2. another character pointer
  3. a function pointer. The function in question returns an int and takes 2 character pointers as arguments.

The next line,

int (*p)(const char *,const char *);

This is a function pointer p. The function returns an int and takes 2 character pointers as arguments.

if(!(cmp)(a,b))

cmp is not defined in the scope here, but I am assuming this line is inside the function check. In that case, you are calling the function cmp with the arguments a and b which are presumably character pointers.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • thanks , but i really wonted to know if its normal for me not to understand this things or i'm learning C wrong – capHossein12DarthVader Jul 16 '19 at 03:42
  • @capHossein12DarthVader If you are just learning pointers then function pointers are a more advanced topic. You should understand the details and intricacies of pointers. StackOverflow does not generally recommend any books for learning C, but there are a few older answers which can give some information. – Rishikesh Raje Jul 16 '19 at 04:40