Someone explain function pointers in C to me

If images are not shown try to refresh the page. If you like this website, please disable any AdBlock software!

You are currently reading a thread in /g/ - Technology

You are currently reading a thread in /g/ - Technology

Thread images: 2

Someone explain function pointers in C to me

>>

Explains function pointers in C to OP.

>>

It is a pointer to a function.

>>

They're a pointer to the subroutine that they abstract. The type and calling convention are responsible for setting up the call correctly.

>>

The trick is you create pointer to specific type of functions for example:

int (*kek)(int); // That's the pointer to a function.

You can then assign function that takes int and returns int to it

for example

int fuck_off(int){

return 0;

}

kek = &fuck_off;

This is used mainly to pass functions as arguments to other functions. For example if you want to calculate integral you can create function that takes function you want to integrate

>>

>>52400328

Lets say you have an integer called "I" with the Adress "A"

So A->I (A points on I)

If you print I is has the format %d.

Now we have an integer pointer int *P with the Adress A that points on some Value I.

A->P->I (The Adress A points on P and the Value in P is the Adress of I so P points on I)

If we print P it has the type %p , for Hex-Adress.

Now we can go Forward and Backward, seen from the position of P, using & and *.

A->P->I becomes (&P)->P->(*P).

The value in &P is A and thats an adress

The value in P is P and thats an adress

The value in *P is I and thats an int

Now you can do things like A->P->B->K->I

Lets say I want the value in I as seen from K:

A->P->B->&K->K->*K

You get it by using *K.

>>

>>52400440

>Now you can do things like A->P->B->K->I

>Lets say I want the value in I as seen from K:

>A->P->B->&K->K->*K

>You get it by using *K.

Also, if you want the value from K as seen from A then its ****A because

A->*A->**A->***A->****A->*****A

>>

>>52400328

>he doesn't understand function pointers

>>

>>52400440

>>52400468

I just realized he talkes about function pointers and not just pointers and that I am a faggot

>>

>>52400409

Continued.

If you have functions like:

double f(double x);

double g(double x);

double h(double x);

etc.

And you want to integrate them you can create function like:

double integral( double(*fun)(double), double from, double to, double dx);

First argument is a function you want to integrate.

Call to function integral could for example:

std::cout << integral(f,0.0,5.0,0.01) << std::endl;

std::cout << integral(g,0.0,5.0,0.01) << std::endl;

std::cout << integral(h,0.0,5.0,0.01) << std::endl;

That way you can create only one function instead of creating stuff like integratef(...),integrateg(...) etc.

>>

>>52400541

>C++

Thread images: 2

Thread DB ID: 424177

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.

This is a 4chan archive - all of the shown content originated from that site. This means that 4Archive shows their content, archived. If you need information for a Poster - contact them.

If a post contains personal/copyrighted/illegal content, then use the post's