aboujouj83
IS-IT--Management
I am having a "segmentation fault" when running the following program. Please let me know how to correct it. Note that the compilation is error free:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUM 23
#define STRING_LN 15
/*defining the structures*/
typedef struct{
char first_name[STRING_LN];
char last_name[STRING_LN];
} name;
typedef struct{
name *player_name;
int number;
double salary;
} player;
typedef struct{
name *coach_name;
double salary;
} coach;
typedef struct{
char team_name[STRING_LN];
player team_player[MAX_NUM];
coach *team_coach;
char location[STRING_LN];
} team;
/*******************************************************************
subroutine to create the team
It will invoked directly when the program starts
*******************************************************************/
team* create_team(char *myteam_name, char *mylocation)
{ team *myteam = (team *)malloc(sizeof(team));
int counter; /* used for initializing the array of players */
if (myteam == NULL){
perror("malloc of myteam"
exit(-1);
}
strcpy (myteam -> team_name, myteam_name);
strcpy (myteam ->location, mylocation);
/* initialize the array of players */
for (counter = 0; counter < MAX_NUM; counter++){
strcpy(myteam -> team_player[counter] . player_name -> first_name, ""
strcpy(myteam -> team_player[counter] . player_name -> last_name, ""
myteam -> team_player[counter] . salary = 0;
}
/* initialize the coach*/
strcpy(myteam -> team_coach -> coach_name -> first_name, ""
strcpy(myteam -> team_coach -> coach_name -> last_name, ""
myteam -> team_coach -> salary = 0;
return myteam;
}
.......
The error occurs in the strcmp() method!!!!! when I call it from main():
int main(int argc, char **argv)
{
char answer; /* to read the choice of the user */
team *mteam;
char newtname[STRING_LN]; /* to store newteamname */
char newtlocation[STRING_LN];/* to store newteamlocation */
char pltodelete[STRING_LN]; /* to store temporarily the name
of the player to delete */
if (argc != 3)
Usage (argv[0]);
mteam = (team *)malloc(sizeof(team));
if (mteam == NULL){
perror("malloc of myteam"
exit(-1);
}
printf("\nCreating the team with the arguments specified "
"in the command line\n"
mteam = create_team(argv[1], argv[2]);
........ THE ERROR OCCURS HERE!!!!!!!! IT SAYS A SEGMENTATION FAULT WHEN TRYING TO CALL STRCMP().IT'S AN ERROR WITH POINTERS AND STRINGS, PLEASE TRY TO FIGURE IT OUT!
GEORGES
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUM 23
#define STRING_LN 15
/*defining the structures*/
typedef struct{
char first_name[STRING_LN];
char last_name[STRING_LN];
} name;
typedef struct{
name *player_name;
int number;
double salary;
} player;
typedef struct{
name *coach_name;
double salary;
} coach;
typedef struct{
char team_name[STRING_LN];
player team_player[MAX_NUM];
coach *team_coach;
char location[STRING_LN];
} team;
/*******************************************************************
subroutine to create the team
It will invoked directly when the program starts
*******************************************************************/
team* create_team(char *myteam_name, char *mylocation)
{ team *myteam = (team *)malloc(sizeof(team));
int counter; /* used for initializing the array of players */
if (myteam == NULL){
perror("malloc of myteam"
exit(-1);
}
strcpy (myteam -> team_name, myteam_name);
strcpy (myteam ->location, mylocation);
/* initialize the array of players */
for (counter = 0; counter < MAX_NUM; counter++){
strcpy(myteam -> team_player[counter] . player_name -> first_name, ""
strcpy(myteam -> team_player[counter] . player_name -> last_name, ""
myteam -> team_player[counter] . salary = 0;
}
/* initialize the coach*/
strcpy(myteam -> team_coach -> coach_name -> first_name, ""
strcpy(myteam -> team_coach -> coach_name -> last_name, ""
myteam -> team_coach -> salary = 0;
return myteam;
}
.......
The error occurs in the strcmp() method!!!!! when I call it from main():
int main(int argc, char **argv)
{
char answer; /* to read the choice of the user */
team *mteam;
char newtname[STRING_LN]; /* to store newteamname */
char newtlocation[STRING_LN];/* to store newteamlocation */
char pltodelete[STRING_LN]; /* to store temporarily the name
of the player to delete */
if (argc != 3)
Usage (argv[0]);
mteam = (team *)malloc(sizeof(team));
if (mteam == NULL){
perror("malloc of myteam"
exit(-1);
}
printf("\nCreating the team with the arguments specified "
"in the command line\n"
mteam = create_team(argv[1], argv[2]);
........ THE ERROR OCCURS HERE!!!!!!!! IT SAYS A SEGMENTATION FAULT WHEN TRYING TO CALL STRCMP().IT'S AN ERROR WITH POINTERS AND STRINGS, PLEASE TRY TO FIGURE IT OUT!
GEORGES