-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
266 lines (242 loc) · 24 KB
/
Copy pathindex.html
File metadata and controls
266 lines (242 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>How Do I Declare A Function Pointer in C?</title>
<meta content='Fucking Function Pointers: Examples for declaring function pointers in C' name='description'>
<meta content='function pointer, C, syntax, programming' name='keywords'>
<meta name=viewport content="width=device-width, initial-scale=1">
<style type="text/css">
body {
background-color: #2b2b2b;
color: #a9b7c6;
font-family: Lucida Grande, sans-serif;
font-size: 20px;
padding: 40px;
}
h1 {
font-size: 40px;
margin-bottom: 1.5em;
text-align: center;
}
h2 {
margin-top: 1.5em;
font-weight: normal;
}
h3 {
font-size: 40px;
margin-top: 0;
margin-bottom: 0.5em;
font-weight: normal;
}
strong {
color: #3162c4;
}
code {
display: block;
font-family: Monaco, Menlo, monospace;
margin-bottom: 1em;
}
pre {
display: block;
font-family: Monaco, Menlo, monospace;
margin: 0;
}
footer {
font-size: 12px;
margin-top: 200px;
opacity: 0.5;
text-align: right;
}
a {
color: #6897bb;
text-decoration: none;
}
.syntax {
margin-bottom: 1em;
margin-left: 40px;
margin-top: 1.5em;
}
.example {
font-size: 15px;
margin-bottom: 1em;
margin-left: 40px;
margin-top: 1em;
}
.disclaimer {
line-height: 1.5em;
margin-top: 3em;
}
.ckeyword {
color: #cc7832;
}
.cidentifier {
color: #9373a5;
}
.cparameters {
color: #908b25;
}
</style>
<style media="print">
body {
background-color: #FFFFFF;
font-size: 16px;
padding: 20px;
}
h1 {
font-size: 28px;
margin-bottom: 1em;
}
footer {
font-size: 10px;
margin-top: 40px;
}
.syntax {
margin-bottom: 1em;
margin-left: 20px;
margin-top: 1.5em;
}
.example {
display: none;
}
</style>
<!-- Favicon follows https://medium.com/web-dev-survey-from-kyoto/favicon-nightmare-how-to-maintain-sanity-7628bfc39918
and image assets generated via ImageMagick from bash script in source repo -->
<link rel="icon" href="favicon.svg" type="image/svg+xml"/>
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"/>
<link rel="manifest" href="/site.webmanifest"/>
<meta name="theme-color" content="#ffffff">
</head>
<body>
<h1>How Do I Declare a Function Pointer in C?</h1>
<div>
<h2>As a <strong>variable</strong>:</h2>
<div class="syntax"><code><span class="ckeyword">returnType</span> (*<span class="cidentifier">variableName</span>)(<span
class="cparameters">parameterTypes</span>) = function_name;</code></div>
<div class="example"> (<a id="variable_example"
href="https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(j:1,options:(compileOnChange:'0'),source:'%23include+%3Cstddef.h%3E%0A%0A/*+Declare+a+simple+function+that+takes+one+integer%0A+++argument+and+returns+an+integer.+*/%0Astatic+int+my_function(int+foo)%3B%0A%0A/*+Define+a+global+variable+named+%22my_function_pointer%22%0A+++whose+type+is+a+pointer+to+a+function+that+takes+one%0A+++integer+argument+and+returns+an+integer.+Initialize%0A+++this+to+NULL.+*/%0Aint+(*my_function_pointer)(int)+%3D+NULL%3B%0A%0Aint+main(void)+%7B%0A++/*+Point+the+function+pointer+to+the+simple+function+*/%0A++my_function_pointer+%3D+my_function%3B%0A%0A++/*+Invoke+the+function+via+the+global+function%0A+++++pointer+variable.+*/%0A++return+my_function_pointer(0)%3B%0A%7D%0A%0A/*+Define+the+simple+%22my_function%22+function.*/%0Astatic+int+my_function(int+foo)+%7B%0A++return+foo%3B%0A%7D%0A'),l:'5',n:'1',o:'C%2B%2B+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang_trunk,filters:(b:'0',commentOnly:'0',directives:'0',intel:'0'),options:'-x+c+--std%3Dc99'),l:'5',n:'0',o:'%231+with+x86-64+clang+(trunk)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4">example
code</a>)
</div>
</div>
<div>
<h2>As a <strong>static const variable</strong>:</h2>
<div class="syntax"><code>static <span class="ckeyword">returnType</span> (* const <span class="cidentifier">variableName</span>)(<span
class="cparameters">parameterTypes</span>) = function_name;</code></div>
<div class="example"> (<a id="static_const_example"
href="https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(j:1,options:(compileOnChange:'0'),source:'/*+Declare+a+simple+function+that+takes+one+integer%0A+++argument+and+returns+an+integer.+*/%0Astatic+int+my_function(int+foo)%3B%0A%0A/*+Define+a+global+variable+named+%22my_function_pointer%22%0A+++whose+type+is+a+pointer+to+a+function+that+takes+one%0A+++integer+argument+and+returns+an+integer.+Limit+its%0A+++visibility+to+this+translation+unit+and+make+it%0A+++const.+Initialize+it+to+point+to+the+simple+function%0A+++declared+above.+*/%0Astatic+int+(*+const+my_function_pointer)(int)+%3D+my_function%3B%0A%0Aint+main(void)+%7B%0A++/*+Invoke+the+function+via+the+global+function%0A+++++pointer+variable.+*/%0A++return+my_function_pointer(0)%3B%0A%7D%0A%0A/*+Define+the+simple+%22my_function%22+function.*/%0Astatic+int+my_function(int+foo)+%7B%0A++return+foo%3B%0A%7D%0A'),l:'5',n:'1',o:'C%2B%2B+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang_trunk,filters:(b:'0',commentOnly:'0',directives:'0',intel:'0'),options:'-x+c+--std%3Dc99'),l:'5',n:'0',o:'%231+with+x86-64+clang+(trunk)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4">example
code</a>)
</div>
</div>
<div>
<h2>As an <strong>array</strong>:</h2>
<div class="syntax"><code><span class="ckeyword">returnType</span> (*<span
class="cidentifier">arrayName</span>[])(<span class="cparameters">parameterTypes</span>) = {function_name0,
...};</code></div>
<div class="example"> (<a id="array_example"
href="https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(j:1,options:(compileOnChange:'0'),source:'/*+Declare+two+simple+functions+that+each+take+one%0A+++integer+argument+and+return+an+integer.+*/%0Aint+my_function1(int+foo)%3B%0Aint+my_function2(int+foo)%3B%0A%0A/*+Define+a+global+variable+named+%22my_function_pointers%22%0A+++whose+type+is+an+array+of+pointers+to+functions+that+%0A+++each+take+one+integer+argument+and+return+an+integer.%0A+++Initialize+this+with+an+array+of+pointers+to+the+%0A+++functions+declared+above.+*/%0Aint+(*my_function_pointers%5B%5D)(int)+%3D+%7Bmy_function1,+my_function2%7D%3B%0A%0Aint+main(void)+%7B%0A++/*+Invoke+the+first+function+in+the+global+function%0A+++++pointer+array.+*/%0A++return+my_function_pointers%5B0%5D(0)%3B%0A%7D%0A%0A/*+Define+the+simple+%22my_function%22+function.*/%0Aint+my_function1(int+foo)+%7B%0A++return+foo%3B%0A%7D%0A%0A/*+Define+the+simple+%22my_function%22+function.*/%0Aint+my_function2(int+foo)+%7B%0A++return+foo%3B%0A%7D%0A'),l:'5',n:'1',o:'C%2B%2B+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang_trunk,filters:(b:'0',commentOnly:'0',directives:'0',intel:'0'),options:'-x+c+--std%3Dc99'),l:'5',n:'0',o:'%231+with+x86-64+clang+(trunk)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4">example
code</a>)
</div>
</div>
<div>
<h2>As a <strong>parameter to a function</strong>:</h2>
<div class="syntax"><code>int my_function(<span class="ckeyword">returnType</span> (*<span class="cidentifier">parameterName</span>)(<span
class="cparameters">parameterTypes</span>));</code></div>
<div class="example"> (<a id="function_parameter_example"
href="https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAKxAEZTUAHAvVAOwGdK0BbRvADaYA8mwDCCAIZtgmEAHIADPICUpDqgCuxZHPkB6AFQBqACKZkAycUzHJxgGaa2yFu2MEpBD5IDWmDg8Ad1Q7YmBNHkw2Ai4AUkUAQWMUlLiAJnTkSQEBACNJZF8M9LtjRlQ8GMxiD1D7Jxc3Ng8vH39A9kwE5NS%2B/tSAN0r0csrq2usIqJi7NlGbAm1OYx6B9dTpYyqCTFliADo1vpLNDhrGa0keErLhvFGKnZqew30eneMeAE8AfUbXKw2BBPhBDNlcgUiioIPdRoY1MY4cZDGcLlceCo4gBmABCPR6RjMFisNjKEPyhV8jmcgPcnkk3gIfgCxi6SJGY2exGOYWm0W80gWmCWxBWW2e%2BwOKPeSU%2BP1%2ByDy/1pzVhnNR52Il2I1yxeIJctmPEkVXVDxUqwA7PikikiWIbIzbBLqvskdY8JI8kJHCRWrYKVDfNK3sd5X8dddVtjTMZFDjbYljkSAJJsYb%2BAM0ppA0jlSQcDhVYDbFr2J4TOoB3kpINUnN08vzbOSmp8yIC0Oy3qLZZfP4AtUKpUq3PsfMZABsCqjmMTBKtpkNiSJ5gcVRdjearUZxnQqFZbFQnhLxjymm8VUztk8gZylKKpF5lyLZ8I2YpNQAtIxiKhBgeTBRimTsYm7D5jUHVUgRBWYwXraFzXhRFkU1dFdXnJI4htY4%2BzFYxEN8CA0W1DF9STHDl2wpI10wDc2C3IjtyBXdvHwlY70Ih8fz/ACgPQXlQJmAgIKNbwR2VIdYLQ0i5yxbDcLtMtvEMHY5xjOM4NUlQ5IxBdlLwBxjAgMAwHU8jrSTPoOOMb9aAM3oqLwkV%2BzUmI50c5yklUUgBAUABWeRSDYBRaGC1AFDEDJ8XSXFjA0bRdFWdJsXC0gCAUZQVDUXwQACxQ/IUAAWYLQqUCKFGCrhCsyiq1DgWAkF4fghDICgIBawQahQKwZF%2BAhiGcXxSA3ARdmILgIDyLLgt4ETRAEb5ZtIfAbEBQYAhW55/PqhhmCBLh5G/AAPQi7O/DgCEE2NkAATju3zdqCkKVsi%2BQMjS4wgkIBBjBOgAOKdvynYrCL60sIEG4aVGCursty/LCt20rXoqhgqvUEBatmnKivkdIyrezH4dUNRNsmoEQGKoA%3D%3D">example
code</a>)
</div>
</div>
<div>
<h2>As a <strong>return value from a function</strong>:</h2>
<div class="syntax"><code><span class="ckeyword">returnType</span> (*my_function(int, ...))(<span
class="cparameters">parameterTypes</span>);</code></div>
<div class="example"> (<a id="function_return_example"
href="https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAKxAEZTUAHAvVAOwGdK0BbRvADaYA8mwDCCAIZtgmEAHIADPICUpDqgCuxZHPkB6AFQBqACKZkAycUzHJxgGaa2yFu2MEpBD5IDWmDjs2Yzw2AkxZYmNGa0keTHCo6XRjAFJFAEFjbJsCbU47R2dXVmDGVFDE4wB3QgQPAE9GW3QtACMhCEMVCFbNDswVADp0rOyAFQQ8QNqBATtXYwE8fyC7YmJJBpC2LAAPUOAilzc2IeNDfVG%2BgeMungaAfScT0ohKlR6boRVUgGYAEKjUZGMwWKw2QocBLGVAOY4ldiBTySbwEPy2dgtdpCNKZbLrYCaeJhIIpXL5QrfTDnS7XHG2B6PZBtZ7FU6PWi9BnRWI8X6A%2Bn9XFMlls17sR4AJm5w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strong>cast</strong> (<a id="mindtribe_blog"
href="https://web.archive.org/web/20160425131024/http://www.mindtribe.com:80/2015/06/do-c-callbacks-like-this-not-like-that/">but
try not to cast functions</a>):</h2>
<div class="syntax"><code>... (<span class="ckeyword">returnType</span> (*)(<span
class="cparameters">parameterTypes</span>))my_expression ...</code></div>
<div class="example"> (<a id="cast_example"
href="https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAKxAEZTUAHAvVAOwGdK0BbRvADaYA8mwDCCAIZtgmEAHIADPICUpDqgCuxZHPkBSAEwBmPG2QDNWANT7jYjgXQC8AIwB0CO9n2KAgr5%2BAPQAVNYAIpgWksSY1pLWvIwxeBps1gBmmuYs7NYEUgTWqYmofJIsrkLWAO6EXv7WTQCOGsQEYPIc1pgAHoxRBJjo1hx4wGwV2pju1gDqcQDWbKg1%2BYW1mJ0CAtaumIFNoyQsMsVsQ7LEHKTHm6MDyHiSLgBecQUHjU1GhgBuqDwIzQnCKIV%2B1mSxEkPEwQ2u8TYI00HDivzMRRBjms4MMhkOTTMjkwknQ7gJtm%2B1gAEphYp1up9rK0ThAVNZ0KhkJpYRcKqx0hkSNZYhk6ZhzHIKQgCARGFwgkEXGxNL13PgZmw4UEeNIgsYgiz2oEQkFAo5%2BchzpiylDMAB9DGYK4cCAY0qgnG3d1YsEqOwAIUCgXdurMEABQPZ%2BgA7EHvqEIpgMmY4tJ4sRoQBPayoDLW510xmoY7tHFm76hrP2lU8fbXfQAViDjfCtmMbdjAfo1gAbLcACwADn71kMA9uxkUjcnikUhluC%2BsiluMdujdj4UDwYTYQAyid1mnM5Ic3mCy7zpCBJJdLcUWZgEeKU0kik0plsshcukanEsCmWrxK4qB/DM1gBlEkgoh8CBfH4RxNGMExTLEHKoJgHBsJ0RR9LeBACDmuoEMgCDMm0HRdD0/SDMML6jOMkwENMtx/tYWrDPkJYwfEiSSNiRDxJGwK2oIdL0TUMRsI%2BHBBHSxAkBwsymgSRoEBAPDVrW9Y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UwkFwemUy6QQDKlw6TtjbGFG0cUVBUyZoqMZSzkr9PQDMrK8zQr1OWdstZQQfijO6tVdInSGq7OarTc6UQGa3PErlQIioxr%2BFUKQAQChGzyFIGwBQtB/moAUGIIwQZDABmONoXQtgTDAtIAQBQygVBqEWCARsK4fnyAHP8wFSgQUKH%2BVwFcyLCVqDgLAJAlchBkAoK0vgVcUA3hkPaEJ2RFikBTAIUBlBXAov%2BbwXkBBRCEUFaQfAsQlpKIlU6HFqKGDMAFFweQ1NeiJGsNTamjhGYdmQAATgNV8nFfyAUStBQYBFtQdG9BHNTXsA5EisqfBADlbBFgqH%2BeS1F6LMXYoUHi81hKGDEvUCAMlgq0XfIUIYfFFqw0%2BtUGoMC1wBQgAHEAA%3D%3D%3D">example
code</a>)
</div>
</div>
<div>
<h2>As a <strong>function pointer typedef</strong>:</h2>
<div class="syntax"><code><span class="ckeyword">typedef returnType</span> (*<span
class="cidentifier">typeName</span>)(<span class="cparameters">parameterTypes</span>);</code></div>
<div class="example"> (<a id="function_pointer_typedef_example"
href="https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(j:1,options:(compileOnChange:'0'),source:'/*+Use+typedef+to+declare+the+name+%22my_function_ptr_t%22%0A+++as+an+alias+whose+type+is+a+pointer+to+a+function+that%0A+++takes+one+integer+argument+and+returns+an+integer+*/%0Atypedef+int+(*my_function_ptr_t)(int)%3B%0A%0A/*+Declare+a+simple+function+that+takes+one+integer%0A+++argument+and+returns+an+integer.+*/%0Aint+my_function(int+foo)%3B%0A%0Aint+main(void)+%7B%0A++/*+Define+a+function+pointer+variable+and+point+it+to%0A+++++the+function.+*/%0A++my_function_ptr_t+my_function_pointer+%3D+my_function%3B%0A++%0A++/*+Invoke+the+function+via+the+function+pointer.+*/%0A++return+my_function_pointer(0)%3B%0A%7D%0A%0A/*+Define+the+simple+%22my_function%22+function.*/%0Aint+my_function(int+foo)+%7B%0A++return+foo%3B%0A%7D%0A'),l:'5',n:'1',o:'C%2B%2B+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang_trunk,filters:(b:'0',commentOnly:'0',directives:'0',intel:'0'),options:'-x+c+--std%3Dc99'),l:'5',n:'0',o:'%231+with+x86-64+clang+(trunk)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4">example
code</a>)
</div>
</div>
<div>
<h2>As a <strong>function typedef</strong>:</h2>
<div class="syntax"><code><span class="ckeyword">typedef returnType</span> <span
class="cidentifier">typeName</span>(<span class="cparameters">parameterTypes</span>);</code></div>
<div class="example"> (<a id="function_typedef_example"
href="https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(j:1,options:(compileOnChange:'0'),source:'/*+Use+typedef+to+declare+the+name+%22my_function_t%22%0A+++as+an+alias+whose+type+is+a+function+that+takes%0A+++one+integer+argument+and+returns+an+integer+*/%0Atypedef+int+my_function_t(int)%3B%0A%0A/*+Declare+a+simple+function+that+takes+one+integer%0A+++argument+and+returns+an+integer.+Here+we+use+the%0A+++typedef+instead+of+typing+out+the+entire+signature.%0A+++This+may+look+a+bit+odd+to+some+developers.+Someone%0A+++more+used+to+function+pointer+typedefs+might+mistake%0A+++it+as+the+definition+of+a+function+pointer+variable.%0A+++So+I!'m+not+sure+I!'d+recommend+it.+But,+as+shown+%0A+++below,+it!'s+possible.+*/%0Astatic+my_function_t+my_function%3B%0A%0Aint+main(void)+%7B%0A++/*+Define+a+function+pointer+variable+and+point+it+to%0A+++++the+function.+*/%0A++my_function_t+*my_function_pointer+%3D+my_function%3B%0A++%0A++/*+Invoke+the+function+via+the+function+pointer.+*/%0A++return+my_function_pointer(0)%3B%0A%7D%0A%0A/*+Define+the+simple+%22my_function%22+function.+*/%0Astatic+int+my_function(int+foo)+%7B%0A++return+foo%3B%0A%7D%0A'),l:'5',n:'1',o:'C%2B%2B+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang_trunk,filters:(b:'0',commentOnly:'0',directives:'0',intel:'0'),options:'-x+c+--std%3Dc99'),l:'5',n:'0',o:'%231+with+x86-64+clang+(trunk)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4">example
code</a>)
</div>
</div>
<h1>How Do I Do It in C++? (C++11 and later)</h1>
<div>
C++ offers several compelling alternatives to C-style function pointers such as templates and <a
href="https://en.cppreference.com/w/cpp/utility/functional/function"
title="C++ reference page for std::function">std::function</a> with <a
href="https://en.cppreference.com/w/cpp/utility/functional/bind"
title="C++ reference page for std::bind">std::bind</a>.
If you really want to use function pointers in C++, you can still use the same C-style syntax shown above or the
type aliases below.
</div>
<div>
<h2>As a <strong>function pointer type alias</strong>:</h2>
<div class="syntax"><code><span class="ckeyword">using</span>
<span class="cidentifier">typeName</span> = <span class="ckeyword">returnType</span> (*)(<span
class="cparameters">parameterTypes</span>);</code></div>
<div class="example"> (<a id="function_pointer_typedef_example"
href="https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:12,endLineNumber:2,positionColumn:12,positionLineNumber:2,selectionStartColumn:12,selectionStartLineNumber:2,startColumn:12,startLineNumber:2),source:'/*+Use+using+to+declare+the+name+%22my_function_ptr_t%22%0A+++as+an+alias+whose+type+is+a+pointer+to+a+function+that%0A+++takes+one+integer+argument+and+returns+an+integer+*/%0Ausing+my_function_ptr_t+%3D+int+(*)(int)%3B%0A%0A/*+Declare+a+simple+function+that+takes+one+integer%0A+++argument+and+returns+an+integer.+*/%0Aint+my_function(int+foo)%3B%0A%0Aint+main(void)+%7B%0A++/*+Define+a+function+pointer+variable+and+point+it+to%0A+++++the+function.+*/%0A++my_function_ptr_t+my_function_pointer+%3D+my_function%3B%0A++%0A++/*+Invoke+the+function+via+the+function+pointer.+*/%0A++return+my_function_pointer(0)%3B%0A%7D%0A%0A/*+Define+the+simple+%22my_function%22+function.*/%0Aint+my_function(int+foo)+%7B%0A++return+foo%3B%0A%7D%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang_trunk,deviceViewOpen:'1',filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'--std%3Dc%2B%2B11',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+(trunk)+(Editor+%231)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4">example
code</a>)
</div>
</div>
<div>
<h2>As a <strong>function type alias</strong>:</h2>
<div class="syntax"><code><span class="ckeyword">using</span>
<span class="cidentifier">typeName</span> = <span class="ckeyword">returnType</span> (<span class="cparameters">parameterTypes</span>);</code>
</div>
<div class="example"> (<a id="function_pointer_typedef_example"
href="https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:20,endLineNumber:11,positionColumn:20,positionLineNumber:11,selectionStartColumn:20,selectionStartLineNumber:11,startColumn:20,startLineNumber:11),source:'/*+Use+using+to+declare+the+name+%22my_function_t%22%0A+++as+an+alias+whose+type+is+a+function+that+takes%0A+++one+integer+argument+and+returns+an+integer+*/%0Ausing+my_function_t+%3D+int(int)%3B%0A%0A/*+Declare+a+simple+function+that+takes+one+integer%0A+++argument+and+returns+an+integer.+Here+we+use+the%0A+++type+alias+instead+of+typing+out+the+entire+signature.%0A+++This+may+look+a+bit+odd+to+some+developers.+Someone%0A+++more+used+to+function+pointer+typedefs+might+mistake%0A+++it+as+the+definition+of+a+function+pointer+variable.%0A+++So+I!'m+not+sure+I!'d+recommend+it.+But,+as+shown+%0A+++below,+it!'s+possible.+*/%0Astatic+my_function_t+my_function%3B%0A%0Aint+main(void)+%7B%0A++/*+Define+a+function+pointer+variable+and+point+it+to%0A+++++the+function.+*/%0A++my_function_t+*my_function_pointer+%3D+my_function%3B%0A++%0A++/*+Invoke+the+function+via+the+function+pointer.+*/%0A++return+my_function_pointer(0)%3B%0A%7D%0A%0A/*+Define+the+simple+%22my_function%22+function.+*/%0Astatic+int+my_function(int+foo)+%7B%0A++return+foo%3B%0A%7D%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang_trunk,deviceViewOpen:'1',filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'--std%3Dc%2B%2B11',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+(trunk)+(Editor+%231)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4">example
code</a>)
</div>
</div>
<div class="disclaimer"> This site is not intended to be an exhaustive list of all possible uses of function
pointers.<br> If you find yourself needing syntax not listed here, it is likely that a <strong>typedef</strong>
would make your code more readable.<br> <br> Unable to access this site due to the profanity in the URL? <strong><a
id="gosh_darned"
href="https://goshdarnfunctionpointers.com">https://goshdarnfunctionpointers.com</a></strong> is a more
work-friendly mirror.
</div>
<footer>By <a id="jerryryle" href="https://jerryryle.com"
title="Jerry's portfolio site">Jerry Ryle</a>, who fucking loves function pointers.<br/> Inspired by Em
Lazer-Walker's <a id="fucking_block_syntax" href="http://fuckingblocksyntax.com">Block Syntax</a>.<br/> <a
id="fucking_github_repo"
href="https://github.com/jerryryle/fuckingfunctionpointers.com"
title="Github repo for this website">Make it better! Send me a pull request!</a><br/>
</footer>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-8TCB5VZQ9M"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-8TCB5VZQ9M');
</script>
</body>
</html>