Skip to content

Commit 4aa15d4

Browse files
committed
Add Lecture 3 material.
1 parent 8cd1c47 commit 4aa15d4

File tree

12 files changed

+135
-77
lines changed

12 files changed

+135
-77
lines changed

fundamentals2/GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PROGRAMS = vars1 vars2 vars3 vars4 ptr struct union addrspace
1+
PROGRAMS = vars1 vars2 vars3 vars4 ptr struct
22

33
all: $(PROGRAMS)
44

fundamentals2/addrspace.c

Lines changed: 0 additions & 24 deletions
This file was deleted.

fundamentals2/ptr.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55

66
int array[10];
77

8-
int main(int argc, char *argv[]) {
9-
(void)argc;
10-
(void)argv;
8+
int main(int argc, char* argv[]) {
9+
(void) argc, (void) argv;
1110

12-
int *p;
11+
int* p = &array[0];
1312

14-
p = &array[0];
15-
16-
printf("Address of array: %p\n", array);
17-
printf("Value of p: %p\n", p);
18-
printf("Value of &p[1]: %p\n", &p[1]);
19-
printf("Value of p + 1: %p\n", p + 1);
20-
return 0;
13+
printf("Address of array: %p\n", array);
14+
printf("Value of p: %p\n", p);
15+
printf("Value of &p[1]: %p\n", &p[1]);
16+
printf("Value of p + 1: %p\n", p + 1);
17+
return 0;
2118
}

fundamentals2/struct.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44
#include <stdlib.h>
55

66
typedef struct {
7-
char name[22];
8-
unsigned int age;
9-
char house[15];
7+
char name[22];
8+
unsigned int age;
9+
char house[15];
1010
} student;
1111

1212
student array_of_students[10];
13-
int main(int argc, char *argv[]) {
14-
student eddie = { "Eddie", 29, "Maxwell Dworkin" };
13+
int main(int argc, char* argv[]) {
14+
student eddie = { "Eddie", 29, "Maxwell Dworkin" };
1515

16-
(void)argc;
17-
(void)argv;
16+
(void) argc, (void) argv;
1817

19-
array_of_students[5] = eddie;
20-
printf("Where is the array? %p\n", array_of_students);
21-
printf("Where is Eddie? %p\n", &eddie);
22-
printf("How large is a struct student???\n");
23-
return 0;
18+
array_of_students[5] = eddie;
19+
printf("Where is the array? %p\n", array_of_students);
20+
printf("Where is Eddie? %p\n", &eddie);
21+
printf("How large is a struct student???\n");
22+
return 0;
2423
}

fundamentals2/union.c

Lines changed: 0 additions & 29 deletions
This file was deleted.

fundamentals3/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
struct
2+
ptr
3+
addrspace
4+
union

fundamentals3/GNUmakefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
PROGRAMS = ptr struct union addrspace
2+
3+
all: $(PROGRAMS)
4+
5+
ALLPROGRAMS = $(PROGRAMS)
6+
7+
include ../common/rules.mk
8+
9+
%.o: %.c $(BUILDSTAMP)
10+
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEPCFLAGS) $(O) -o $@ -c $<
11+
12+
%: %.o
13+
$(CC) $(CFLAGS) $(O) -o $@ $^
14+
15+
clean:
16+
rm -f $(ALLPROGRAMS) *.o
17+
rm -rf $(DEPSDIR) *.dSYM
18+
19+
.PHONY: all clean

fundamentals3/addrspace.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
char global_ch = 'B';
5+
const char const_global_ch = 'C';
6+
7+
int main(int argc, char* argv[]) {
8+
(void) argc, (void) argv;
9+
10+
char local_ch = 'A';
11+
const char const_local_ch = 'E';
12+
char *heap_ch = (char*) malloc(1);
13+
*heap_ch = 'D';
14+
15+
printf("Address of a global variable\t\t%p\n", &global_ch);
16+
printf("Address of a const global variable\t%p\n", &const_global_ch);
17+
printf("Address of a local variable\t\t%p\n", &local_ch);
18+
printf("Address of a const local variable\t%p\n", &const_local_ch);
19+
printf("Address of a heap variable\t\t%p\n", heap_ch);
20+
printf("Address of main\t\t\t\t%p\n", main);
21+
return 0;
22+
}

fundamentals3/ptr.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Data Representation examples; addresses of shorts
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
int array[10];
7+
8+
int main(int argc, char* argv[]) {
9+
(void) argc, (void) argv;
10+
11+
int* p = &array[0];
12+
13+
printf("Address of array: %p\n", array);
14+
printf("Value of p: %p\n", p);
15+
printf("Value of &p[1]: %p\n", &p[1]);
16+
printf("Value of p + 1: %p\n", p + 1);
17+
return 0;
18+
}

fundamentals3/struct.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Data Representation examples
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
typedef struct {
7+
char name[22];
8+
unsigned int age;
9+
char house[15];
10+
} student;
11+
12+
student array_of_students[10];
13+
int main(int argc, char* argv[]) {
14+
student eddie = { "Eddie", 29, "Maxwell Dworkin" };
15+
16+
(void) argc, (void) argv;
17+
18+
array_of_students[5] = eddie;
19+
printf("Where is the array? %p\n", array_of_students);
20+
printf("Where is Eddie? %p\n", &eddie);
21+
printf("How large is a struct student???\n");
22+
return 0;
23+
}

0 commit comments

Comments
 (0)