-
Notifications
You must be signed in to change notification settings - Fork 1
/
smth.c
94 lines (56 loc) · 1.47 KB
/
smth.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
#include <stdio.h>
#include <stdlib.h>
long *makeLongArray() {
long *test = (long *) malloc(sizeof(long) * 10);
//long *test1 = (long*) malloc(sizeof(int*));
for (int i = 0; i < 11; i++) {
*(test + i) = i;
}
//*test1 = 32;
return test;
}
char *charpointer() {
char *res = malloc(1);
*res = 'h';
return res;
}
void *mallocling() {
void *e = malloc(sizeof(int *));
void *d = malloc(2);
printf("%d \n", e);
printf("%d", e + 1);
printf("\n %d \n", e + 2);
printf("%d \n", d);
printf("%d", d + 1);
}
//one way to init a 2d array {the manual way}
char *make2darray1() {
int r = 3, c = 4;
char *arr = (char *) malloc(r * c * sizeof(char));
int i, j, count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++) {
*(arr + i * c + j) = ++count; // do stuff and arr[r][c]
}
return arr;
}
//way to make an 2d array
char *make2darray2() {
int r = 3, c = 4, i, j, count;
char *arr = (char *) malloc(sizeof(char *) * r);
for (i = 0; i < r; i++)
*(arr + i) = (char *) malloc(c * sizeof(char));
// Note that arr[i][j] is same as *(*(arr+i)+j)
count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
*((arr + i) + j) = ++count; // Or *(*(arr+i)+j) = ++count
return arr;
}
int main() {
char *arr = make2darray2();
for (int i = 0; i < 13; i++) {
printf("%d \n", *(arr + i));
}
return 0;
}