[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Someone explain function pointers in C to me

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 11
Thread images: 2

File: 1450103342586.jpg (98KB, 808x805px) Image search: [Google]
1450103342586.jpg
98KB, 808x805px
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
>>
File: 1452092658416.jpg (20KB, 460x276px) Image search: [Google]
1452092658416.jpg
20KB, 460x276px
>>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 posts: 11
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
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 content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.