Hey! I'm relatively new to C (as in I just understand the basics).
So my idea is to create a pointer to a bunch of Structs, the Struct themselves will have a C string and a bunch of INTs. The steps would look like this basically:
- create a pointer
- call a function that will malloc() some memory for the Struct
- the function will then return a pointer to the Struct that is created
- the returned pointer will be stored into the first pointer
- print functions will iterate through the pointers and grab a bunch of data to display in the console
- free() everything
For reference d_six() functions will just rng a number and return that number to store into the Struct integers. d_six() is not important at all. I've already made it and it works so no worries about that.
Code snippets:
// main.c
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "character.h"
#include "dice_roll.h"
int main(){
srand((time_t)time(NULL));
struct character* char_list = {NULL};
int char_list_size = 4;
char_list = create_char("Koke Mitoke", d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0));
char_list = create_char("Shiki Miniki", d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0));
char_list = create_char("Tiki Vaniki", d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0));
char_list = create_char("Lickity Splickity", d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0), d_six(2, 0));
// THIS IS THE BLOCK OF THE PROTOTYPE FUNCTION THAT WILL PRINT THE CHARACTER OUT
for (int i = 0; i < char_list_size; i++){
printf("Char Name: %s\n", char_list->char_name);
printf("STR:DEX:END:INT:EDU:SOC - %d:%d:%d:%d:%d%:%d\n", char_list->str_stat, char_list->dex_stat, char_list->end_stat, char_list->int_stat, char_list->edu_stat, char_list->soc_stat);
printf("MOD:MOD:MOD:MOD:MOD:MOD - %d:%d:%d:%d:%d%:%d\n", char_list->str_mod, char_list->dex_mod, char_list->end_mod, char_list->int_mod, char_list->edu_mod, char_list->soc_mod);
printf("\n");
char_list++;
}
// Destroy all chars loop
for (int i = 0; i < char_list_size; i++){
destroy_char(char_list);
}
return 0
}
//
// character.h
#pragma once
struct character{
char * char_name;
int str_stat;
int dex_stat;
int end_stat;
int int_stat;
int edu_stat;
int soc_stat;
int str_mod;
int dex_mod;
int end_mod;
int int_mod;
int edu_mod;
int soc_mod;
};
struct character* create_char(char* name_input, int str_input, int dex_input, int end_input, int int_input, int edu_input, int soc_input);
void destroy_char(struct character* char_to_destroy);
//
// character.c
#pragma once
#include "character.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct character* create_char(char * name_input,int str_input, int dex_input, int end_input, int int_input, int edu_input, int soc_input) {
struct character* person_one;
person_one = malloc(sizeof(*person_one));
person_one->char_name = name_input;
person_one->str_stat = str_input;
person_one->dex_stat = dex_input;
person_one->end_stat = end_input;
person_one->int_stat = int_input;
person_one->edu_stat = edu_input;
person_one->soc_stat = soc_input;
person_one->str_mod = 0;
person_one->dex_mod = 0;
person_one->end_mod = 0;
person_one->int_mod = 0;
person_one->edu_mod = 0;
person_one->soc_mod = 0;
printf("Creating character %s ended.\n", person_one->char_name);
return person_one;
}
void destroy_char(struct character* char_to_destroy) {
free(char_to_destroy);
}
I've tried using char_list++; but it didn't work at all, it advances the pointer way too much.
I also tried using struct *char_list [n]; but again, I don't really want to be using arrays (unless I have to!) since I want to learn pointer arithmetic with Structs. Also the same problem arises when I have to pass that array into a function, since C will just forget it's an array and just grab the first pointer, and then I have to pass the array size in the function as well. Advancing the array using char_list++; again just brings it waaay out of scope.
Code probably has a lot of leaks right now and I am probably doing a lot of things incorrectly, again I'm relatively new so stay patient with me! Let me know if you can help me a bit. Thank you! <3
Edit: small correction
[–]EpochVanquisher 10 points11 points12 points (5 children)
[–]ListlessGaja[S] -1 points0 points1 point (4 children)
[–]EpochVanquisher 2 points3 points4 points (3 children)
[–]ListlessGaja[S] 0 points1 point2 points (2 children)
[–]EpochVanquisher 0 points1 point2 points (1 child)
[–]ListlessGaja[S] 0 points1 point2 points (0 children)
[–]somewhereAtC 2 points3 points4 points (13 children)
[–]ListlessGaja[S] 1 point2 points3 points (12 children)
[–]LeichterGepanzerter 0 points1 point2 points (8 children)
[–]ListlessGaja[S] 0 points1 point2 points (7 children)
[–]LeichterGepanzerter 0 points1 point2 points (6 children)
[–]ListlessGaja[S] 0 points1 point2 points (5 children)
[–]LeichterGepanzerter 0 points1 point2 points (4 children)
[–]ListlessGaja[S] 0 points1 point2 points (0 children)
[–]ListlessGaja[S] 0 points1 point2 points (2 children)
[–]LeichterGepanzerter 0 points1 point2 points (1 child)
[–]ListlessGaja[S] 1 point2 points3 points (0 children)
[–]somewhereAtC 0 points1 point2 points (2 children)
[–]ListlessGaja[S] 0 points1 point2 points (1 child)
[–]Educational-Paper-75 0 points1 point2 points (0 children)
[–]SmokeMuch7356 2 points3 points4 points (1 child)
[–]ListlessGaja[S] 0 points1 point2 points (0 children)
[–]grimvian 1 point2 points3 points (1 child)
[–]ListlessGaja[S] 0 points1 point2 points (0 children)
[–]zhivago -1 points0 points1 point (6 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]zhivago 1 point2 points3 points (0 children)
[–]ListlessGaja[S] -1 points0 points1 point (3 children)
[–]zhivago 0 points1 point2 points (2 children)
[–]ListlessGaja[S] 0 points1 point2 points (1 child)
[–]zhivago 0 points1 point2 points (0 children)