I need to write a program that will recieve (from the user) the number of players in a basketball team and then I need to create an array in a dynamic way. The program will sort the array in an alphabetic order and then print it like that aswell. I wrote this code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define LENGTH 20
int main(void)
{
int numplayers, i, j;
char persname[LENGTH], tname[LENGTH], temp[LENGTH];
printf("Please insert the amount of basketball players in the group\n");
scanf("%d", &numplayers);
char **players = (char **) malloc(numplayers * sizeof(char *));
printf("Please insert the names of your %d basketball players\n", numplayers);
for (i = 0; i < numplayers; i++) {
gets(persname);
strcpy(persname[i], tname[i]);
}
for (i = 0; i < numplayers-1; i++) {
for (j = i+1; j < numplayers; j++) {
if (strcmp(persname[i], persname[j])) {
strcpy(temp[i], persname[i]);
strcpy(persname[i], persname[j]);
strcpy(persname[j], temp);
}
}
}
for (i = 0; i < numplayers; i++) {
printf("%s\t\t%s\n", tname[i], persname[i]);
}
return 0;
}
But when I run the code, I get an error right after typing the amount of players in the team, Unhandled exception at 0x507340E3 (msvcr120d.dll) in Question4.exe: 0xC0000005: Access violation reading location 0xFFFFFFCC.
What did I do wrong.