Can't understand pointer operator

I am making a ctf problem and there is a line I cannot figure out.

int  (*fp)(char *)=(int(*)(char *))&puts, i;

      

Can someone explain to me what this means?

+3


source to share


3 answers


fp

- pointer

(*fp)

      

to function

(*fp)(

      

which takes 1 argument like char

(*fp)(char)

      



and returns a value like int

int (*fp)(char)

      

The pointer is initialized with an address puts

after a major redundant conversion.

int  (*fp)(char *)=(int(*)(char *))&puts
int  (*fp)(char *)=(int(*)(char *))puts // & redundant
int  (*fp)(const char *)=puts

      

The object is i

not initialized. It is of typeint

int  (*fp)(char *)=(int(*)(char *))&puts, i;

      

+4


source


First, the variable declaration appears:

int  (*fp)(char *)

      

fp

is a pointer to a function that takes a parameter char *

and returns int

.

Then it is fp

initialized with a value:



(int(*)(char *))&puts

      

The value is the address of the function puts

, passed in the same type as fp

.

And finally, there is another variable declaration:

int /* ... */, i;

      

0


source


The ad has two parts:

int  (*fp)(char *)=(int(*)(char *))&puts, i;

      

first : int (*fp)(char *)=(int(*)(char *))&puts;

Explanation: This is a function pointer declaration and initialization in one expression. Where fp

is a function pointer puts

. If you print the values fp

and puts

, they will have the same meaning, that is, the address puts

.

#include<stdio.h>

int main()
{
  int  (*fp)(char *)=(int(*)(char *))&puts, i;
  printf("puts %p\n",puts);
  printf("fp %p\n",fp);
}

      

and the second one :int i;

0


source







All Articles