feat: update structure
This commit is contained in:
42
cs2106/labs/lab1/part2/lab1p2a.c
Normal file
42
cs2106/labs/lab1/part2/lab1p2a.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int *p1, *p2, *p3, *p4;
|
||||
|
||||
void fun1(int x, int y) {
|
||||
|
||||
static int w;
|
||||
int z;
|
||||
w = x * y;
|
||||
z = x + y;
|
||||
|
||||
printf("Inside fun1\n");
|
||||
p1 = &w;
|
||||
p2 = &x;
|
||||
p3 = &y;
|
||||
p4 = &z;
|
||||
printf("Address of p1 = %p, address of p2 = %p, address of p3 = %p, address of p4 = %p\n", &p1, &p2, &p3, &p4);
|
||||
printf("Address of w = %p, address of x = %p, address of y = %p, address of z = %p\n", p1, p2, p3, p4);
|
||||
printf("w = %d, x = %d, y = %d, z = %d\n", w, x, y, z);
|
||||
}
|
||||
|
||||
|
||||
int fun2(int f, int g, int h) {
|
||||
printf("\nInside fun2\n");
|
||||
printf("w = %d, x = %d, y = %d, z = %d\n", *p1, *p2, *p3, *p4);
|
||||
return f + g + h;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int a = 5, b = 6, c;
|
||||
|
||||
fun1(a, b);
|
||||
|
||||
printf("\nOutside fun1\n");
|
||||
printf("w = %d, x = %d, y = %d, z = %d\n", *p1, *p2, *p3, *p4);
|
||||
|
||||
fun2(1, 2, 3);
|
||||
printf("\nOutside fun2\n");
|
||||
printf("w = %d, x = %d, y = %d, z = %d\n", *p1, *p2, *p3, *p4);
|
||||
}
|
||||
21
cs2106/labs/lab1/part2/lab1p2b.c
Normal file
21
cs2106/labs/lab1/part2/lab1p2b.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int accumulate(int x) {
|
||||
// Modify this function so that it accumulates values passed to it to
|
||||
// a value called "acc".
|
||||
|
||||
static int acc = 0;
|
||||
|
||||
acc = acc + x;
|
||||
printf("acc is now %d\n", acc);
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
// Call fun2 to accumulate from 1 to 10
|
||||
for(int i=1; i<=10; i++)
|
||||
accumulate(i);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user