Skip to content

Commit 55cf3e2

Browse files
committed
Add Day 16: Union, enum and void pointer
1 parent 9628207 commit 55cf3e2

File tree

6 files changed

+154
-1
lines changed

6 files changed

+154
-1
lines changed

Diff for: Day 16/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Day 16 Programs
2+
3+
1. [Understanding Unions and Enumerations](enum.c)
4+
2. [Struct, union and enum all at once](fruitOrder.c)
5+
3. [Void pointer](voidPointers.c)
6+
4. [Program for generic sum function (to debgug)](genericSum.c)

Diff for: Day 16/enumAndUnion.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Understanding enum and union in C
3+
* and also how we can use enums to
4+
* know which field of the union has been stored in the memory
5+
*/
6+
7+
#include <stdio.h>
8+
9+
enum week_day_type {NUMBER, STRING};
10+
11+
typedef union {
12+
int week_no;
13+
char week_str[10];
14+
} week_day;
15+
16+
int main () {
17+
18+
// week_day_type is our custom data type
19+
// that can take only the values from the list declared
20+
21+
enum week_day_type currentDay = STRING;
22+
// printf("%d", currentDay);
23+
24+
week_day tuesday = {.week_str = "TUESDAY"};
25+
if (currentDay == STRING) {
26+
printf("Current day is: %s\n", tuesday.week_str);
27+
}
28+
else {
29+
printf(" %d\n", tuesday.week_no);
30+
}
31+
32+
return 0;
33+
}

Diff for: Day 16/fruitOrder.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Program with enum, struct and union
3+
*/
4+
5+
#include <stdio.h>
6+
7+
typedef enum {
8+
COUNT, POUNDS, PINTS
9+
} unit_of_measure;
10+
11+
typedef union {
12+
short count;
13+
float weight;
14+
float volume;
15+
} quantity;
16+
17+
typedef struct {
18+
const char *name;
19+
const char *country;
20+
// union type: quantity
21+
quantity amount;
22+
// enum type: unit_of_measure
23+
unit_of_measure units;
24+
} fruit_order;
25+
26+
void display(fruit_order order) {
27+
printf("This order contains ");
28+
if (order.units == PINTS) {
29+
printf("%2.2f pints of %s\n", order.amount.volume, order.name);
30+
} else if (order.units == POUNDS) {
31+
printf("%2.2f lbs of %s\n", order.amount.weight, order.name);
32+
} else {
33+
printf("%i %s\n", order.amount.count, order.name);
34+
}
35+
}
36+
37+
int main() {
38+
fruit_order apples = {"apples", "England", .amount.count = 144, COUNT};
39+
40+
fruit_order strawberries = {"strawberries", "Spain", .amount.weight = 17.6, POUNDS};
41+
42+
fruit_order oj = {"orange juice", "U.S.A", .amount.volume = 10.5, PINTS};
43+
44+
display(apples);
45+
display(strawberries);
46+
display(oj);
47+
return 0;
48+
}

Diff for: Day 16/genericSum.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Using void pointers to create a generic function
3+
*/
4+
5+
// TRY TO DEBUG THIS PROGRAM, IT GIVES INCORRECT OUTPUT
6+
7+
#include <stdio.h>
8+
9+
// add function takes in two numbers and prints the result
10+
// type = 1 => int, type = 2 => float
11+
void add(void *num1, void *num2, int type) {
12+
if (type == 1) {
13+
int val1 = *((int *)num1);
14+
int val2 = *((int *)num2);
15+
printf("The sum is: %d\n", val1 + val2);
16+
} else if (type == 2) {
17+
float val1 = *((float *)num1);
18+
float val2 = *((float *)num2);
19+
printf("The sum is: %f\n", val1 + val2);
20+
}
21+
return ;
22+
}
23+
24+
int main () {
25+
void *a, *b, *c, *d;
26+
27+
int num1 = 2;
28+
a = &num1;
29+
30+
int num2 = 3;
31+
b = &num2;
32+
33+
float num1f = 2.35;
34+
float num2f = 5.1;
35+
c = &num1f;
36+
d = &num2f;
37+
38+
// add two integers
39+
add(&a, &b, 1);
40+
// add two floats
41+
add(&c, &d, 2);
42+
43+
return 0;
44+
}

Diff for: Day 16/voidPointers.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Program to understand void pointers in C
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main () {
8+
9+
void *ptr;
10+
int a = 3;
11+
// make the pointer ptr point to an integer
12+
ptr = &a;
13+
// must type case void pointer to dereference it
14+
printf("The value stored in object pointed by ptr is: %d\n",*((int*)ptr));
15+
return 0;
16+
}

Diff for: README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,10 @@ Simple program using if: [if](Day 3/if.c)
118118
## Day 15: Structures in C
119119
1. [Declaring and using structure](Day 15/structFish.c)
120120
2. [Nested structures](Day 15/nestedStructures.c)
121-
3. [Updating structure fields](Day 15/turtleBirthday.c)
121+
3. [Updating structure fields](Day 15/turtleBirthday.c)
122+
123+
## Day 16 Unions, Enum and Void pointers
124+
1. [Understanding Unions and Enumerations](Day 16/enum.c)
125+
2. [Struct, union and enum all at once](Day 16/fruitOrder.c)
126+
3. [Void pointer](Day 16/voidPointers.c)
127+
4. [Program for generic sum function (to debgug)](Day 16/genericSum.c)

0 commit comments

Comments
 (0)