"is there no way you can use an array for this?"
The arrays char name[] at the end of the struct fighter and struct team structures are of incomplete type. This is allowed as the last member of a struct, creating a flexible array member (or FAM). But, a struct with a member of incomplete type is itself of incomplete type. C does not allow creation of arrays with elements of incomplete type.
The easiest way to solve this problem is to choose a constant size for the name arrays which is large enough to accommodate expected names and to use regular array members of constant size to store the names. Before initializing these arrays the lengths of names should be validated and names which are too long handled.
#include <stdio.h>
#include <string.h>
#define NAME_SZ 20 // Small size for testing purposes
#define MAX_FIGHTERS 100
struct fighter {
int type;
int strength;
int dexterity;
int resistance;
int weapon;
char name[NAME_SZ];
};
struct fighter fighters[MAX_FIGHTERS];
struct fighter genf(const char *name) {
// Ensure that all members are zero initialized.
struct fighter genfighter = { 0 };
size_t name_sz = strlen(name) + 1; // add 1 for `\0` terminator
// Can only copy at most `NAME_SZ` characters.
if (name_sz > NAME_SZ) {
name_sz = NAME_SZ;
}
memcpy(genfighter.name, name, name_sz);
// Add terminating `\0` if needed.
if (name_sz == NAME_SZ) {
genfighter.name[NAME_SZ-1] = '\0';
}
return genfighter;
}
int main(void) {
char names [][NAME_SZ] = {
"Apocalypse Alan",
"Burly Barry",
"Cederick the Clever",
};
size_t names_num = sizeof names / sizeof *names;
size_t fighters_num = 0;
// Populate `fighters` array.
for (size_t i = 0; i < names_num; i++) {
fighters[i] = genf(names[i]);
++fighters_num; // Increment number of fighters.
}
// Let's try to add a couple of names that are too long.
fighters[fighters_num++] = genf("Cederick the Cleaver");
fighters[fighters_num++] = genf("Derrick of Doubtful Kindness");
// Show names of fighters in `fighters` array.
for (size_t i = 0; i < fighters_num; i++) {
printf("Fighter #%zu: %s\n", i+1, fighters[i].name);
}
return 0;
}
Compiling and running the above program yields:
$ gcc -std=c17 -Wall -Wextra -pedantic fighterstructs.c
$ ./a.out
Fighter #1: Apocalypse Alan
Fighter #2: Burly Barry
Fighter #3: Cederick the Clever
Fighter #4: Cederick the Cleave
Fighter #5: Derrick of Doubtful
Note that the last two names that were added, "Cederick the Cleaver" and "Derrick of Doubtful Kindness", are too long to be stored in a names array (which at present holds 20 chars, or a string of maximum length 19). But the genf function checked to see if the name would fit in a names array before applying memcpy, only copied the initial portion of the name that would fit, and made sure that the result was null-terminated. "Cederick the Cleaver" is only one char too long; the array holding this string must be 21 chars long. If this was copied without checking length then code would write past the end of the name array, causing undefined behavior. Copying only the first 20 chars avoids a buffer overflow, but the resulting array is not null-terminated. An additional check must be made to ensure that a null-terminated array is created.
Finally, make sure that you always compile code with a generous number of warnings enabled. A good start is gcc -std=c17 -Wall -Wextra -pedantic.
namebe a FAM? That will add complexity.scanf("%s");struct fighter fighters[];You can also not have arrays without size except as last member of a struct. An array without elements does not make any sense.-Wall -Wextra -pedantic. That should at least warn about missing parameters forscanf.