Why does this code give an error (C program)? int findteam(char t[4], team…
- User
Why does this code give an error (C program)? int find_team(char t[4], team teams_arr[NUM_OF_TEAMS], int *added_teams) { printf("%s\n", t); for (int i = 0; i < *added_teams; i++) { printf("%d %s\n", i, teams_arr[i].name); if (strcmp(t, teams_arr[i].name) == 0) { return i; } } (*added_teams)++; teams_arr[*added_teams].name = t; return 0; }
- Assistant
The error is likely because you're trying to assign a string (
t) toteams_arr[*added_teams].name, which is an array of characters.In C, you cannot assign a string to an array using the assignment operator (
=). You need to use a function likestrcpy()orstrncpy()to copy the contents of one string to another.To fix the error, you can change the line
teams_arr[*added_teams].name = t;tostrcpy(teams_arr[*added_teams].name, t);orstrncpy(teams_arr[*added_teams].name, t, sizeof(teams_arr[*added_teams].name));depending on the length of the strings you are working with.