-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionPointerExample.c
More file actions
61 lines (49 loc) · 1.24 KB
/
Copy pathfunctionPointerExample.c
File metadata and controls
61 lines (49 loc) · 1.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
#include <stdio.h>
/*
* William Murphy
* 4/21/2018
*
* Example of how to read
* and understand the different
* types of pointers.
*/
// FUNC 1
/* arr_len: function
* args:
* int size
* returns:
* int
* */
int arr_len(int size){return size;}
// FUNC 2
/* arr: function
* args:
* function pointer:
* arr_len: function
* args:
* int size
* int s
* returns:
* pointer to an int
*
*
*/
int * arr(int (*arr_len)(int), int s){
int length = arr_len(s);
int array[length];
int* arrPtr;
arrPtr = array;
return arrPtr;
}
int main() {
int * rs;
rs = arr(arr_len, 5); // note: arr[] and *arr are the same and thus, either one can be
// be used in function declarations.
*(rs) = 1; // *(rs) points to array[0], *(rs+1) points to array[1] and so on
*(rs + 1) = 10;
*(rs + 2) = 100;
*(rs + 3) = 9;
*(rs + 4) = 10000;
printf("result[0] = %d\nrs[1] = %d\nrs[2] = %d\nrs[3]=%d\nrs[4] = %d\n",*(rs),*(rs+1),*(rs+2),*(rs+3),*(rs+4));
return 0;
}