source:
CIVL/examples/languageFeatures/pointers.cvl@
ba6c728
| Last change on this file since ba6c728 was ea777aa, checked in by , 3 years ago | |
|---|---|
|
|
| File size: 799 bytes | |
| Rev | Line | |
|---|---|---|
| [36b5ada] | 1 | /* Commandline execution: |
| 2 | * civl verify pointers.cvl | |
| 3 | * */ | |
| [e6b02c8] | 4 | #include<civlc.cvh> |
| [6543229] | 5 | |
| [4ceb0ef] | 6 | typedef struct Node { |
| 7 | int id; | |
| 8 | struct Node* next; | |
| 9 | } Node; | |
| 10 | ||
| [40a899c] | 11 | void passByRef(int * array) { |
| 12 | *array = 42; | |
| 13 | } | |
| 14 | ||
| [6543229] | 15 | void main() { |
| 16 | int a; | |
| 17 | int* b; | |
| 18 | int c[2]; | |
| 19 | int* d; | |
| [b6565a0] | 20 | int** g; |
| [40a899c] | 21 | int* f; |
| [4ceb0ef] | 22 | int *k[5]; |
| 23 | $scope myscope = $here; | |
| 24 | Node head, tail; | |
| 25 | Node *hp, *tp; | |
| [b6565a0] | 26 | |
| [4ceb0ef] | 27 | tail.id = 1; |
| 28 | tail.next = NULL; | |
| 29 | tp = &tail; | |
| 30 | head.id = 0; | |
| 31 | head.next = tp; | |
| 32 | for(int i = 0; i < 3; i++){ | |
| 33 | k[i] = (int *) $malloc(myscope, sizeof(int)); | |
| 34 | *k[i] = i; | |
| 35 | } | |
| 36 | k[3] = &a; | |
| 37 | k[4] = &c[0]; | |
| [40a899c] | 38 | f = &a; |
| [6543229] | 39 | a = 1; |
| 40 | b = &a; | |
| 41 | c[0] = 0; | |
| 42 | c[1] = 1; | |
| 43 | d = &c[1]; | |
| [b6565a0] | 44 | g = &d; |
| [d980649] | 45 | $assert(*b == 1); |
| 46 | $assert(*d == 1); | |
| 47 | $assert(**g == 1); | |
| 48 | $assert(g == &d); | |
| [40a899c] | 49 | passByRef(f); |
| [d980649] | 50 | $assert(*f == 42); |
| [4ceb0ef] | 51 | $free(k[0]); |
| 52 | $free(k[1]); | |
| 53 | $free(k[2]); | |
| [6543229] | 54 | } |
Note:
See TracBrowser
for help on using the repository browser.
