#include // For printf #include // For malloc #include"er_in2060.h" // header fileFrim int main(){ /********** Part 1: Variables **********/ // In C variables are weakly typed int a = 4; char b = 'h'; printf("4 + h is: %i\n", a+b); /********** Part 2: If/else Statements **********/ if(b > a){ a += 4; } else{ a += 2; } printf("%i\n", a); /********** Part 3: Pointers **********/ int x = 8; int y = 3; printf("Content (x:y): %d : %d\n", x, y); printf("Address (&x:&y): %p : %p\n", &x, &y); int* z; z = &x; printf("Pointer to an integer (z): %p\n", z); // z hold the address of x printf("Pointer to a pointer (&z): %p\n", &z); //&z hold the address of z printf("Derreferenced pointer (*z): %d\n", *z); // *z is the value of x printf("(*(&z)): %p\n", *(&z)); // *(&z) is the value of z, which is the address of x printf("(&(*z)): %p\n", &(*z)); // &(*z) is the address of x int* my_pointer = NULL; printf("Null pointer: %p\n", my_pointer); if (my_pointer == NULL) { printf("'my_pointer' is empty\n"); } int* pointer = malloc(sizeof(int)*1); pointer[0] = 3; printf("A pointer: %x\n", pointer); free(pointer); /********** Part 4: Strings and memory allocation **********/ // Strings are represented by placing several chars after each other in memory // Memory must be allocated for a string char* string = "Hello!"; char* string_2 = malloc(sizeof(char)*7); char* start_string_2 = string_2;// save the start of the memory block string_2[0] = 'H'; string_2[1] = 'e'; string_2[2] = 'l'; string_2[3] = 'l'; string_2[4] = 'o'; string_2[5] = '!'; string_2[6] = 0; printf("%s\n", string_2); // Allocate a 3x3 matrix int* matrix_data = malloc(sizeof(int)*3*3); int** matrix = malloc(sizeof(int*)*3); for(int i = 0; i < 3; i++){ matrix[i] = &matrix_data[i*3]; } //Indexes int x1 = 0; int y1 = 2; //Assignation matrix[x1][y1] = 128; //Access through indexes printf("Matrise [x1][y1] = %d\n", matrix[x1][y1]); //Check strukt.c example /********** Part 5: Loops **********/ for(int i = 0; i < 6; i++){ printf("%i\n", i); } while(*string_2){ printf("%c\n", string_2[0]); string_2 += 1; //pointer is being moved } /********** Part 6: Functions and header files **********/ printf("Er det IN2060? %s\n", er_IN2060("IN2060")); free(start_string_2);//We must free from the begining of the allocated memory block }