-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointers.c
39 lines (29 loc) · 875 Bytes
/
pointers.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
# include <string.h> // strlen
# include <stdio.h> // printf
# include <stdlib.h> // malloc
int main() {
int i = 12345;
int *j = &i; // j is a pointer to an integer
printf("%d\n", i);
printf("0x%x\n", j);
*j = 88; // follow the pointer, makes i = 88
printf("%d\n", i);
// *j is an alias of i
// an alias is another name for a variable or a storage area
// j = 0; // segmentation fault (crash, you suck)
// *j = 12345; // definitely not allowed
char *str;
// allocate memory for the string
// str[0] = 'h';
char *tmp = "Hello world"; // a C string constant (array of bytes)
str = malloc(strlen(tmp)); // allocate however many bytes tmp is
char *saved = str;
// copy tmp in to str
while (*tmp != '\0'){
*str = *tmp;
str++; // advance the pointers to next byte
tmp++;
}
*str = '\0';
printf("DEBUG %s\n", saved);
}