Skip to content

Commit 0ef7953

Browse files
authored
Add files via upload
1 parent 2f2adbf commit 0ef7953

File tree

9 files changed

+201
-0
lines changed

9 files changed

+201
-0
lines changed

ptr_to_struct/struct1.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// gcc -o struct1 struct1.c
2+
3+
#include <stdio.h>
4+
5+
struct point {
6+
int x;
7+
int y;
8+
};
9+
10+
int main() {
11+
struct point p;
12+
13+
p.x = 10;
14+
p.y = 42;
15+
16+
printf("x is %d and y is %d\n", p.x, p.y);
17+
}

ptr_to_struct/struct2.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// gcc -o struct2 struct2.c
2+
3+
#include <stdio.h>
4+
5+
struct point {
6+
int x;
7+
int y;
8+
};
9+
10+
int main() {
11+
struct point p = {11, 43};
12+
13+
printf("x is %d and y is %d\n", p.x, p.y);
14+
}

ptr_to_struct/struct3.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// gcc -o struct3 struct3.c
2+
3+
#include <stdio.h>
4+
5+
struct point {
6+
int x;
7+
int y;
8+
};
9+
10+
int main() {
11+
struct point q = {12, 44};
12+
struct point p;
13+
14+
p = q;
15+
16+
printf("x is %d and y is %d\n", p.x, p.y);
17+
}

ptr_to_struct/struct3a.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// gcc -o struct3a struct3a.c
2+
3+
#include <stdio.h>
4+
5+
struct point {
6+
int x;
7+
int y;
8+
};
9+
10+
int main() {
11+
struct point q = {12, 44};
12+
struct point p;
13+
14+
p = q;
15+
p.x = p.x + 1;
16+
p.y = p.y + 1;
17+
18+
printf("q: x is %d and y is %d\n", q.x, q.y);
19+
printf("p: x is %d and y is %d\n", p.x, p.y);
20+
}

ptr_to_struct/struct4.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// gcc -o struct4 struct4.c
2+
3+
#include <stdio.h>
4+
5+
typedef struct point {
6+
int x;
7+
int y;
8+
} point_t;
9+
10+
int main() {
11+
point_t p = {13, 45};
12+
13+
printf("x is %d and y is %d\n", p.x, p.y);
14+
}

ptr_to_struct/struct5.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// gcc -o struct5 struct5.c
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
typedef struct point {
8+
int x;
9+
int y;
10+
char desc64[64];
11+
} point_t;
12+
13+
int main() {
14+
point_t *p = (point_t *) malloc(sizeof(point_t));
15+
16+
p->x = 14;
17+
p->y = 46;
18+
strcpy(p->desc64, "A point");
19+
20+
printf("x is %d and y is %d and it is a '%s' and its size is %d\n",
21+
p->x, p->y, p->desc64, (int) sizeof(point_t));
22+
23+
free(p);
24+
}

ptr_to_struct/struct6.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// gcc -o struct6 struct6.c
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
typedef struct point {
8+
int x;
9+
int y;
10+
char desc64[64];
11+
} point_t;
12+
13+
int main() {
14+
point_t *p = (point_t *) malloc(sizeof(point_t));
15+
point_t *q;
16+
17+
p->x = 14;
18+
p->y = 46;
19+
strcpy(p->desc64, "A point");
20+
21+
printf("x is %d and y is %d and it is a '%s' and its size is %d\n",
22+
p->x, p->y, p->desc64, (int) sizeof(point_t));
23+
24+
q = p;
25+
26+
q->x = 15;
27+
q->y = 47;
28+
29+
printf("x is %d and y is %d\n", p->x, p->y);
30+
31+
free(p);
32+
// free(q) /* NO */
33+
}

ptr_to_struct/struct7.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// gcc -o struct7 struct7.c
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
typedef struct point {
8+
int x;
9+
int y;
10+
char desc64[64];
11+
} point_t;
12+
13+
void point_factor2(point_t *apoint) {
14+
apoint->x = apoint->x * 2;
15+
apoint->y = apoint->y * 2;
16+
}
17+
18+
int main() {
19+
point_t *p = (point_t *) malloc(sizeof(point_t));
20+
21+
p->x = 14;
22+
p->y = 46;
23+
strcpy(p->desc64, "A point");
24+
25+
point_factor2(p);
26+
27+
printf("x is %d and y is %d\n", p->x, p->y);
28+
29+
free(p);
30+
}

ptr_to_struct/struct8.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// gcc -o struct8 struct8.c
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
typedef struct point {
8+
int x;
9+
int y;
10+
char desc64[64];
11+
} point_t;
12+
13+
void point_add(point_t *a, const point_t *b) {
14+
a->x = a->x + b->x;
15+
a->y = a->y + b->y;
16+
// b->x = 0; /* No, as b is const */
17+
}
18+
19+
int main() {
20+
point_t *p1 = (point_t *) malloc(sizeof(point_t));
21+
point_t p2 = {10, 20, "Another point"};
22+
23+
p1->x = 14;
24+
p1->y = 46;
25+
strcpy(p1->desc64, "A point");
26+
27+
point_add(p1, &p2);
28+
29+
printf("x is %d and y is %d\n", p1->x, p1->y);
30+
31+
free(p1);
32+
}

0 commit comments

Comments
 (0)