This is a snippet of the code from a project made in the programming class at my college, and my problem is that I get a segmentation fault error when I get to the strcpy
part and I have no idea why.
I don't know if it's relevant or not, but I am coding in vs code under linux.
Here's the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct Teams {
char country[20];
char sponsor[20];
char group;
int points;
} E;
char *sponsor_generator(char *country) {
int i, k = 0;
char sp[20];
for (i = strlen(country) - 1; i >= 0; i--) {
sp[k] = country[i];
k++;
}
sp[k] = '\0';
return sp;
}
void read(E *ec, int *n) {
(*n)++;
printf("Country: ");
scanf("%s", (ec + *n)->country);
(ec + *n)->group = (ec + *n)->country[0];
do {
printf("Number of points: ");
scanf("%d", &(ec + *n)->points);
} while ((ec + *n)->points >= 10);
strcpy((ec + *n)->sponsor, sponsor_generator((ec + *n)->country));
}
int main() {
int n = -1;
E ec[64];
read(ec, &n);
return 0;
}
I tried to look up any solutions, but I didn't find something that would work.