0
  1. What does int *(cmp)(char*, char*); mean?

  2. What is the difference between char* ptr1; and char *ptr2;

6
  • 1
    1) see here 2) just writing style. Commented Mar 9, 2020 at 9:56
  • 1
    1. int *(cmp)(char*, char*); is simply int *cmp(char*, char*);: cmp is a function that takes two arguments and both of type char * and returns a pointer to int. 2. No difference. It's matter of choice. Commented Mar 9, 2020 at 10:02
  • 1
    In the second I prefer char *ptr2; because in char* ptr2, ptr3; the ptr3 is not a pointer, it is char ptr3; Commented Mar 9, 2020 at 10:10
  • 3
    Since it's called cmp (compare?), I'd be inclined to think the signature is actually int (*cmp)(char*, char*);, i.e. a pointer to a function which returns an int, as a parameter for a qsort-style comparison function (as explained here). This thread might be useful if you want to see how to write such a function. Commented Mar 9, 2020 at 10:30
  • 1
    @WeatherVane Simply don't write multiple declarations on a single line - doing so is a well-known safety hazard. Not just for pointers; I've also seen things like int a,b,c = 0; when the programmer meant to set all items to zero, not just c. Commented Mar 9, 2020 at 10:36

1 Answer 1

2

this

int *(cmp)(char*, char*); 

is a declaration of a function that has the return type int * and two parameters of the type char *.

You may enclose a declarator in parentheses. So the above function declaration can be also rewritten like

int * ( (cmp)(char*, char*) ); 

The both declarations are equivalent to

int * cmp(char*, char*); 

A declaration of a pointer to such a function will look like

int * ( *p_cmp )(char*, char*) = cmp; 

There is no difference between these declarations

char* ptr1; char *ptr1; char * ptr1; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.