![]() |
|
[C] What does this code means? - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: [C] What does this code means? (/Thread-C-What-does-this-code-means) |
[C] What does this code means? - Boomslang - 08-28-2014 [ Code: python.js" MyAttr="RootTheSystem was here" href="http://churchofcamel.com/alert]
int (*fp)(char *)=(int(*)(char *))&puts, i;RE: [C] What does this code means? - ArkPhaze - 08-31-2014 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. RE: [C] What does this code means? - Legolas - 08-31-2014 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");
}RE: [C] What does this code means? - Boomslang - 08-31-2014 I didn't know that you can declare pointer for functions. Anyway, thanks for explanations
|