init commit
This commit is contained in:
22
structus/Makefile
Normal file
22
structus/Makefile
Normal file
@@ -0,0 +1,22 @@
|
||||
#OBJS specifies which files to compile as part of the project
|
||||
OBJS = main.c
|
||||
|
||||
#CC specifies which compiler to use
|
||||
CC = clang
|
||||
|
||||
#COMPILER_FLAGS specifies the additional compilation options we're using
|
||||
# -w suppress all warnings
|
||||
COMPILER_FLAGS = -Wall
|
||||
|
||||
#LINKER_FLAGS specifies the libraries we're linking against
|
||||
LINKER_FLAGS =
|
||||
|
||||
#OBJ_NAME specifies the name of our executable
|
||||
OBJ_NAME= struct
|
||||
|
||||
#This is the target that compiles our executable
|
||||
all : $(OBJS)
|
||||
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
|
||||
clean :
|
||||
rm struct
|
||||
|
43
structus/main.c
Normal file
43
structus/main.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int gain(int current, int amount_gained);
|
||||
|
||||
void gain_by_pointer(int *current, int amount_gained);
|
||||
|
||||
int main(){
|
||||
struct players{
|
||||
int life;
|
||||
int magic;
|
||||
};
|
||||
|
||||
struct players player1;
|
||||
player1.life = 5;
|
||||
player1.magic = 0;
|
||||
printf(" Hi\n player1's life is currently: %d\n", player1.life);
|
||||
printf(" player1's magic is currently: %d\n", player1.magic);
|
||||
|
||||
player1.magic = gain(player1.magic, 5);
|
||||
player1.life = gain(player1.life, 32);
|
||||
|
||||
printf("\n player1's new magic is %d: \n", player1.magic);
|
||||
|
||||
printf(" player1's new life is %d: \n", player1.life);
|
||||
|
||||
printf(" Adding 1 to both life and magic through pointer magic! \n");
|
||||
|
||||
gain_by_pointer(&player1.magic, 1);
|
||||
gain_by_pointer(&player1.life, 1);
|
||||
|
||||
printf("\n Thanks to pointer magic, life is now %d, magic is now, %d\n", player1.life, player1.magic);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void gain_by_pointer(int *current, int amount_gained){
|
||||
*current += amount_gained;
|
||||
}
|
||||
int gain(int current, int amount_gained){
|
||||
int new_current;
|
||||
new_current = current + amount_gained;
|
||||
return new_current;
|
||||
}
|
BIN
structus/struct
Executable file
BIN
structus/struct
Executable file
Binary file not shown.
Reference in New Issue
Block a user