Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

segmentation fault!

Status
Not open for further replies.

aboujouj83

IS-IT--Management
May 9, 2003
52
US
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(&quot;malloc of myteam&quot;);
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, &quot;&quot;);
strcpy(myteam -> team_player[counter] . player_name -> last_name, &quot;&quot;);
myteam -> team_player[counter] . salary = 0;
}
/* initialize the coach*/
strcpy(myteam -> team_coach -> coach_name -> first_name, &quot;&quot;);
strcpy(myteam -> team_coach -> coach_name -> last_name, &quot;&quot;);
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(&quot;malloc of myteam&quot;);
exit(-1);
}

printf(&quot;\nCreating the team with the arguments specified &quot;
&quot;in the command line\n&quot;);
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
 
i havent gone thro' your code entirely but i guess the
error is out here ...
U have declared

coach *team_coach; in your structure team
This is a pointer to the structure, coach.
So on usage u'll have to allocate memory before u perform operations.
But u have

/* initialize the coach*/
strcpy(myteam -> team_coach -> coach_name -> first_name, &quot;&quot;);
strcpy(myteam -> team_coach -> coach_name -> last_name, &quot;&quot;);
myteam -> team_coach -> salary = 0;

done these operations . But where does the team_coach point to ?? the contents of it will be junk if u havent initilaised to any default value
Hence the seg. fault

-vs
 
Please don't create new threads containing basically the same question
thread205-591656
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top