#include <stdio.h> #include <stdlib.h> #include <string.h> // for strlen() struct name{ char* f_name; char* l_name; }; struct name* create_struct(char* str1, char* str2){ struct name* name_ptr = malloc(sizeof(struct name)); // malloc the space to store the first name char* s1 = malloc(strlen(str1) + 1); // malloc the space to store the last name char* s2 = malloc(strlen(str2) + 1); //copy the str1 string into s1, including that \0 memcpy(s1, str1, strlen(str1) + 1); //copy the str2 string into s2, including that \0 memcpy(s2, str2, strlen(str2) + 1); // make the fields of name_ptr point to the new strings name_ptr->f_name = s1; name_ptr->l_name = s2; return name_ptr; } int main(){ struct name* my_name = create_struct("Jon", "Snow"); printf("My name is %s %s\n", my_name->f_name, my_name->l_name); }