[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Someone explain function pointers in C to me
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

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

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

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.