-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab8.c
135 lines (113 loc) · 3.33 KB
/
lab8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*Max Waldt
Lab 8
CSC 311
4/16/15
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char outprompt;
int *ip;
double *dp;
//Part 1
printf("Part 1\n\n");
int i1;
int i2;
double d1;
double d2;
printf("\nAddress of i1 = %p", &i1);
printf("\nAddress of i2 = %p", &i2);
printf("\nAddress of d1 = %p", &d1);
printf("\nAddress of d2 = %p", &d2);
//Part 2
printf("\n\nPart 2\n\n");
int *intptr1;
int *intptr2;
double *dubptr1;
double *dubptr2;
printf("\nAddress of i1 = %p", intptr1);
printf("\nAddress of i2 = %p", intptr2);
printf("\nAddress of d1 = %p", dubptr1);
printf("\nAddress of d2 = %p", dubptr2);
//Part 3
printf("\n\nPart 3\n\n");
intptr1 = &i1;
intptr2 = &i2;
dubptr1 = &d1;
dubptr2 = &d2;
printf("\nAddress of i1 = %d", intptr1);
printf("\nAddress of i2 = %d", intptr2);
printf("\nAddress of d1 = %d", dubptr1);
printf("\nAddress of d2 = %d", dubptr2);
//Part 4
printf("\n\nPart 4\n\n");
intptr1 = intptr2;
printf("\nValue of intptr1 = %d", intptr1);
dubptr1 = (double*)intptr1;
printf("\nValue of dubptr1 = %p", dubptr1);
printf("\nValue of intptr1 = %p");
//Part 5
printf("\n\nPart 5\n\n");
intptr1 = NULL;
printf("\n%d\n", intptr1);
//Part 6
printf("\n\nPart 6\n\n");
*intptr2 = 5;
printf("\nDereference inptr1: %d");
printf("\nSegmentation fault for intptr1");
//*intptr1 = 5;
//printf("\n%p\n", intptr1);
//Part 7
printf("\n\nPart 7\n\n");
*intptr2 = 100;
printf("\nValue of i1 = %d", i1);
printf("\nValue of i2 = %d", i2);
//Part 8
printf("\n\nPart 8\n\n");
printf("\nintptr2 + 1 = %d\n", (intptr2 + 1));
printf("\ndubptr2 + 1 = %d\n", (dubptr2 + 1));
printf("\nintptr2 + 1 = %d\n", *(intptr2 + 1));
printf("\ndubptr2 + 1 = %d\n", *(dubptr2 + 1));
printf("\nintptr2 - 1 = %d\n", (intptr2 - 1));
printf("\ndubptr2 - 1 = %d\n", (dubptr2 - 1));
printf("\nintptr2 - 1 = %d\n", *(intptr2 - 1));
printf("\ndubptr2 - 1 = %d\n", *(dubptr2 - 1));
//Part 9
printf("\n\nPart 9\n\n");
printf("\nSet intptr1 = &i1, check if intptr1 = intptr2");
intptr1 = &i1;
if (intptr1 == intptr2)
printf("\nintptr1 == intptr2\n");
else printf("\nintptr1 != intptr2\n");
printf("\nSet intptr1 = &i1, check if *intptr1 = *intptr2\n");
if (*intptr1 == *intptr2)
printf("\n* intptr1 == * intptr2\n");
else printf("\n* intptr1 != * intptr2\n");
printf("\nSet intptr1 = &i2, check if intptr1 = intptr2");
intptr1 = &i2;
if (intptr1 == intptr2)
printf("\nintptr1 == intptr2\n");
else printf("\nintptr1 != intptr2\n");
printf("\nSet intptr1 = &i1, check if *intptr1 = *intptr2\n");
if (*intptr1 == *intptr2)
printf("\n* intptr1 == * intptr2\n");
else printf("\n* intptr1 != * intptr2\n");
//Part 10
printf("\n\nPart 10\n\n");
printf("\nMalloc assign a pointer to pi or 3.1416");
double *ptr = (double*)malloc(sizeof(double));
double pi = 3.1416;
ptr = π
printf("\nptr = %6.4f", ptr);
printf("\n*ptr = %6.4f", *ptr);
//Part 11
printf("\n\nPart 11\n\n");
printf("Deallocate ptr");
ptr = (double*)malloc(sizeof(double));
free((void*)ptr);
ptr = (double*)malloc(sizeof(double));
printf("\n*ptr = %6.4f", *ptr);
printf("\n\n");
return 0;
}