source: CIVL/examples/languageFeatures/memcpy.cvl@ c3eca1d

1.23 2.0 main test-branch
Last change on this file since c3eca1d was 313b4a0, checked in by Tim Zirkel <zirkeltk@…>, 13 years ago

Expanded memcpy example.

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

  • Property mode set to 100644
File size: 988 bytes
Line 
1#include<civlc.h>
2
3typedef struct foo {
4 int f0;
5 double f1;
6 int f2;
7 long double f3;
8} foo;
9
10typedef struct bar{
11 foo f0;
12 int f1[8];
13} bar;
14
15void main() {
16 int a[10], b[5];
17 foo foo1, foo2;
18
19 // Basic array component.
20 for (int i = 0; i < 10; i++) {
21 a[i] = i;
22 }
23 memcpy(b, a, 5*sizeof(int));
24 for (int i = 0; i < 5; i++) {
25 $assert b[i] == i;
26 }
27
28 // Basic struct copy.
29 foo1.f0 = 3;
30 foo1.f1 = 3.14;
31 foo1.f2 = 0;
32 foo1.f3 = 2.71828;
33 memcpy(&foo2, &foo1, sizeof(foo));
34 $assert foo2.f0 == 3;
35 $assert foo2.f1 == 3.14;
36 $assert foo2.f2 == 0;
37 $assert foo2.f3 == 2.71828;
38
39 // Modify struct fields.
40 bar bar1;
41 memcpy(&(bar1.f0), &foo2, sizeof(foo));
42 memcpy(&(bar1.f1[3]), b, 5*sizeof(int));
43 $assert bar1.f0.f0 == 3;
44 $assert bar1.f1[3] == 0;
45 memcpy(bar1.f1, a, 4*sizeof(int));
46 $assert bar1.f1[0] == 0;
47 $assert bar1.f1[3] == 3;
48 $assert bar1.f1[5] == 2;
49 $assert bar1.f0.f1 == 3.14;
50 bar1.f0.f1 = 9.99;
51 $assert foo2.f1 == 3.14;
52}
Note: See TracBrowser for help on using the repository browser.