c-test/struct_test.c

56 lines
1.2 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "struct_test.h"
// https://www.w3schools.com/c/c_structs.php
void print_struct(){
struct TestStruct s1;
s1.name = "Kelson";
s1.age = 23;
// https://stackoverflow.com/questions/10162152/how-to-work-with-string-fields-in-a-c-struct
// TestStruct *u = malloc(sizeof(struct TestStruct));
//
// strcpy(s1.name, "Kelson");
// strcpy(s1.age, 23);
// Oh, I had to add strdup to this for it to print.
// 11-4-2024 @ 2:38PM
printf("Name: %s\n", strdup(s1.name));
printf("Age: %d\n", s1.age);
}
#ifdef _TEST
// Not sure how to use this.
patient *create_patient(int number, char *name,
char *addr, char*bd, char sex) {
// Allocate memory for the pointers themselves and other elements
// in the struct.
patient *p = malloc(sizeof(patient));
p->number = number;
// Must allocate memory for contents of pointers. Here, strdup()
// creates a new copy of name. Another option:
// p->name = malloc(strlen(name)+1);
// strcpy(p->name, name);
p->name = strdup(name);
p->address = strdup(addr);
p->birthdate = strdup(bd);
p->gender = sex;
return p;
}
#endif //_TEST