Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions 01-introduction/05-struct/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: clean

structs.o: structs.c
$(CC) -c $(CFLAGS) -o $@ $<

clean:
rm -f structs.o structs
34 changes: 34 additions & 0 deletions 01-introduction/05-struct/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Working with structs
## Motivation
As your programs become more complex you may need structures that hold more than one variable. So let us introduce structs.

## Task
Before you can use a struct you need to define it. Now what does that mean? Let's look at an example:
<br>

```c
struct food {
char name[10];
int calories;
int price;
};
```

We now defined a struct type `food` that we can use similarly to a variable. It can save three values: An array of characters and two integers. We can manipulate it pretty much the same way as any other variable but have a handy container for values that belong together. <br>
To access values inside a struct you need the name of the struct followed by a `.` followed by the name of that variable.

```c
struct food cherry; //defining a struct of the type food named cherry
strcpy(cherry.name, "Cherry"); //accessing the variable name inside the struct using .
cherry.calories = 50;
cherry.price = 69;
};
```

<br>


Look at the example in structs.c.
<br> <br>
Define two more food instances of your choice and fill them with values.
Define a new type of struct `meal` that contains three `food` structs and and an array of characters to give a name to the meal. Create an instance of that struct and fill it with your food instances and look at the values using printf. Try accessing everything from your `meal` struct.
22 changes: 22 additions & 0 deletions 01-introduction/05-struct/structs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <string.h>

int main()
{

struct food {
char name[10];
int calories;
int price;
};


struct food cherry;
strcpy(cherry.name, "Cherry");
cherry.calories = 50;
cherry.price = 69;

printf("The name of the product is %s. For 100g calories are %d and the price is %d cents.\n", cherry.name, cherry.calories, cherry.price);

return 0;
}
11 changes: 11 additions & 0 deletions 01-introduction/06-pointers/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: clean

point.o: point.c
$(CC) -c $(CFLAGS) -o $@ $<

pointers.o: pointers.c
$(CC) -c $(CFLAGS) -o $@ $<

clean:
rm -f point.o point
rm -f pointers.o pointers
45 changes: 45 additions & 0 deletions 01-introduction/06-pointers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Working with pointers
## Motivation
Gain basic understanding of and some practice with pointers.

## Tasks
What are pointers? <br>
Pointers, as the name suggests, point to a certain memory address. Let's start with an example. First we create a variable.
```c
int age = 21;
```
The value of this variable is currently 21. This value is stored at a memory address that you usually don't get to see. Now let's create a pointer compatible to this variable. Use the same data type with an added `*`.
```c
int * p;
```
Currently this pointer does not have a meaningful value as it is not connected to our variable. It is like an arrow pointing somewhere into the blue. Luckily we can now tie the tip of our arrow to the variable we are interested in using `&`.
```c
p = &age;
```
Now, if you print `p` to the terminal you will get the memory adress where the `21` held in `age` is stored. If you want to access the value stored at the memory adress your pointer leads to you again use `*`. As an example let's change the value of `age` using the pointer.
```c
*p = 30;
```
Take a look at `point.c` to see all this in action.
<br>
<br>

Now take a look at pointers.c and make some changes to it. Use only the pointer to the outside struct to make the changes.
<br>
- Change `dwarf_ptr` by giving it the value of `dwarf[]`. Note that when referencing pointers in structs instead of the usual `.` you will use `->` as shown on line 33 and 34. Also note that when pointing to an array you only need to point at the element `[0]`.
<br>
- Change the value of `best_Character` to what is stored in `favorite[]`.
<br>
- The struct `inside` is part of the struct `outside`. Change the value of the array in the inside struct. Again use the pointer to the outside struct to access it. Try changing the value via:
- struct inside -> array
- struct inside -> pointer to array
- pointer to struct inside -> array
- pointer to struct inside -> pointer to array
<br>
- Print the changed vallue accessing it via:
- struct outside -> struct inside -> location
- struct outside -> struct inside -> pointer to location_
- pointer to struct outside -> struct inside -> location
- pointer to struct outside -> struct inside -> pointer to location
- pointer to struct outside -> pointer to struct inside -> location
- pointer to struct outside -> pointer to struct inside -> pointer to location.
20 changes: 20 additions & 0 deletions 01-introduction/06-pointers/point.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <math.h>
#include <stdio.h>
#include <string.h>

int main()
{
int age = 21;

int* p;
p = &age;
printf("%p is the address\n", (void*)p);
printf("%d is the value\n", age);

*p = 30;

printf("%d is the new value\n", age);
printf("%d is also the new value\n", *p);

return 0;
}
42 changes: 42 additions & 0 deletions 01-introduction/06-pointers/pointers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <string.h>

int main()
{
struct inside {
char location[10];
char* location_ptr;
};

struct outside {
char best_Character[10];
char* dwarf_ptr;
struct inside setting;
struct inside* setting_ptr;
};

struct outside Hobbit;
strcpy(Hobbit.best_Character, "Thorin");
Hobbit.dwarf_ptr = &Hobbit.best_Character[0];

// Try only using this pointer for all required changes, not the struct itself.
struct outside* Hobbit_ptr = &Hobbit;

char favorite[] = "Bilbo";
char dwarf[] = "Bombur";


printf("access directly: %s\n", Hobbit_ptr->best_Character);
printf("access via pointer: %s\n", Hobbit_ptr->dwarf_ptr);

struct inside chapter1;
strcpy(chapter1.location, "Shire");
Hobbit.setting = chapter1;
Hobbit.setting_ptr = &Hobbit.setting;
Hobbit.setting.location_ptr = &Hobbit.setting.location[0];

char new_location[] = "Mirkwoord";


return 0;
}