source: CIVL/examples/languageFeatures/pointerAdd.cvl

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.5 KB
Line 
1#include <civlc.cvh>
2#include <stdio.h>
3
4$input double a1d[10];
5$input double a2d[10][10];
6$input double a3d[10][10][10];
7$input double a4d[5][10][15][20];
8
9$scope root = $here;
10
11void main(){
12 double * p1d;
13 double ** p2d;
14 double *** p3d;
15
16
17 p1d = (double *)$malloc(root, sizeof(double) * 10);
18 p2d = (double **)$malloc(root, sizeof(double *) * 10);
19 p3d = (double ***)$malloc(root, sizeof(double **) * 10);
20 for(int i=0; i<10; i++){
21 p1d[i] = i;
22 p2d[i] = (double *)$malloc(root, sizeof(double) * 10);
23 p3d[i] = (double **)$malloc(root, sizeof(double *) * 10);
24 for(int j=0; j<10; j++){
25 p2d[i][j] = i * 10 + j;
26 p3d[i][j] = (double *)$malloc(root, sizeof(double) * 10);
27 for(int k=0; k<10; k++){
28 p3d[i][j][k] = i * 100 + j * 10 + k;
29 }
30 }
31 }
32
33 /* access array */
34 $assert(*(a1d+5) == a1d[5]);
35 $assert(*(*(a2d + 5) + 5) == a2d[5][5]);
36 $assert(**(a2d + 5) == a2d[5][0]);
37 $assert(*(a2d[0] + 9) == a2d[0][9]);
38 $assert(*(&a2d[0][0] + 9) == a2d[0][9]);
39 $assert(*(&a3d[1][2][3] - 40) == a3d[0][8][3]);
40 $assert(*(&a3d[1][2][3] + 145) == a3d[2][6][8]);
41 $assert(*(&a4d[1][2][3][4] + 145) == a4d[1][2][10][9]);
42
43 /* access malloced pointer */
44 $assert(*(p1d + 5) == p1d[5]);
45 $assert(*(*(p2d + 5) + 5) == p2d[5][5]);
46 $assert(*(*(p2d + 9)) == p2d[9][0]);
47 $assert(*(&p3d[0][0][0] + 8) == p3d[0][0][8]);
48
49 $free(p1d);
50 for(int i = 0; i<10; i++){
51 $free(p2d[i]);
52 for(int j = 0; j<10; j++){
53 $free(p3d[i][j]);
54 }
55 $free(p3d[i]);
56 }
57 $free(p2d);
58 $free(p3d);
59}
Note: See TracBrowser for help on using the repository browser.