Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this C pointer code do?

Tags:

c

pointers

I've just came up with this code on minute 1:06 of a coursera lecture. What does it do?

int (*cmp)(char*, char*);
like image 781
Ali Shakiba Avatar asked Dec 03 '22 12:12

Ali Shakiba


2 Answers

This is a pointer to a function where the function returns an int and takes as argument two character pointers.


The basic rule boils down to few things:-

There are three simple steps to follow:

  • Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements: [X] or []
    1. Array X size of... or Array undefined size of...(type1, type2)
    2. function passing type1 and type2 returning... *
    3. pointer(s) to... Keep doing this in a spiral/clockwise direction until all tokens have been covered. Always resolve anything in parenthesis first!

Reference: 1.Clockwise-rule 2.right-left rule

like image 170
user2736738 Avatar answered Dec 12 '22 20:12

user2736738


When you read C declarations you must read them butrophedonically (common way of writing in stone in Ancient Greece).

pointer to
  function that 
    has (char*, char*) type parameters as input
    and int as output

EDIT:

like image 32
alinsoar Avatar answered Dec 12 '22 22:12

alinsoar