-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpointer_example.c
49 lines (36 loc) · 1.38 KB
/
pointer_example.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
#include <stdio.h>
void check_pointer() {
/* declare an integer variable, length, and assign it the value 123 */
int length = 123;
/* declare an "pointer to int" variable, length_ptr */
int *length_ptr;
/* output the value of length */
printf ("length, value %d\n", length);
/* output the address of length */
printf ("length, address %p\n", &length);
/* assign the variable length_ptr the value "&length",
which is the address of length */
length_ptr = &length;
/* output the value of length_ptr -
which really should be the same as &length as printed before*/
printf ("length_ptr, value %p\n", length_ptr);
/* output the value of what length_ptr points -
to do this, we must dereference it*/
printf ("length_ptr, value %d\n", *length_ptr);
printf ("-------------------------\n");
/* assign a new value to the memory points to -
to do this, we must dereference it*/
*length_ptr = 678;
/* output the value of length */
printf ("length, value %d\n", length);
/* output the value of what length_ptr points -
to do this, we must dereference it*/
printf ("length_ptr, value %d\n", *length_ptr);
/* output the address of length_ptr -
which is an address NOT the same as the address of length */
printf ("length_ptr, address %p\n", &length_ptr);
}
int main() {
check_pointer();
return 0;
}