Created
June 16, 2017 05:36
-
-
Save kencoba/73f867d57111e29489f31a3ec7599af0 to your computer and use it in GitHub Desktop.
Variable declarations of pointer to function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
// func1 returns a char. | |
char func1() | |
{ | |
return 'a'; | |
} | |
// func2 returns a pointer to char. | |
char *func2() | |
{ | |
return "world"; | |
} | |
typedef struct { char array[1]; } CharArray; | |
// func3 returns a pointer to CharArray. | |
CharArray (*func3()) | |
{ | |
return &(CharArray) { {'c'} }; | |
} | |
// fun4 returns a CharArray | |
CharArray func4() | |
{ | |
return (CharArray) { {'d'} }; | |
} | |
int main() | |
{ | |
char a[] = "hello"; | |
char *(p[2]) = {"hello","world"}; // p is an array of pointer to char. | |
char *p2[2] = {"hello","world"}; // p2 is an array of pointer to char. | |
char (*q)[] = {&a}; // q is a pointer to array of char. | |
char (*f)() = &func1; // f is a pointer to function returning char. | |
char *(*g)() = &func2; // g is a pointer to function returning pointer to char. | |
CharArray *(*h)() = &func3; // h is a pointer to function returning pointer to CharArray | |
CharArray (*i)() = &func4; | |
printf("%s\n", p[0]); | |
printf("%s\n", p2[0]); | |
printf("%s\n", *q); | |
printf("%c\n", f()); | |
printf("%s\n", g()); | |
printf("%c\n", h()->array[0]); | |
printf("%c\n", i().array[0]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment