Login Register






[C] What does this code means? filter_list
Author
Message
[C] What does this code means? #1
[
Code:
python.js" MyAttr="RootTheSystem was here" href="http://churchofcamel.com/alert] int (*fp)(char *)=(int(*)(char *))&puts, i;
Fuck You.

Reply

RE: [C] What does this code means? #2
It declares fp as a function pointer to the function puts() which takes in a char* and returns an int, and declares i as an integer. Try:
Code:
#include <windows.h> #include <stdio.h> int main(void) { int (*fp)(char *)=(int(*)(char *))&puts, i; i = 77; printf("%d\n", i); fp("test"); }

Now, normally, the prototype for puts() is:
Code:
int puts ( const char * str );

Notice the const in there.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: [C] What does this code means? #3
This line means what ArkPhaze said.

Spoiler:

> Break it, into two lines.

Code:
int (*fp)(char *)=(int(*)(char *))&puts; int i;

> If you want to understand better the first line, you should read about Pointers first. ( And specifically Function Pointers)

> &puts is the same as puts.

> The parameter is designated normally as const char *, rather than char * .

So, the first line is better written like this way.


Code:
int (*fp)(const char *) = puts;

And finally :

Code:
int puts(const char *s); int main(void) { int (*fp)(const char *) = puts; int i; fp("Test"); }

[Image: T4OUWZ1.png]


Reply

RE: [C] What does this code means? #4
I didn't know that you can declare pointer for functions. Anyway, thanks for explanations Smile
Fuck You.

Reply