typedef struct Movie{
char hallName[50];
char movieName[50];
}movie;
I have dynamically created movie array. In processCommand
function:
char *hallName;
hallName = strtok(NULL," ");
and call createHall
function with this arguments.
createHall(&halllName);
then in createHall
function I create movie struct element and assign values here. I want to copy hallName
to Movie.hallName
. I do this strcpy but I get an segmentation fault in linux. In windows this code work correctly.
strcpy(Movie.hallName, *hallName);
How can I fix this problem ?
EDIT:
int main(int argc, char *argv[])
{
FILE *inputFile = fopen(argv[1], "r+");
FILE *outputFile = fopen("output.txt","w+");
char *line=NULL;
movie *Movies = (movie*)malloc((hallNumber)*sizeof(movie));
while(1) {
line = readLine(inputFile);
if (line == NULL)
break;
processCommand(line,outputFile,Movies,hallNumber);
}
free(Movies);
closeFiles(inputFile,outputFile);
return 1;
}
void processCommand(char *line) {
char *hallName = NULL, *command = NULL;
command = strtok(line, " ");
if (strcmp(command, "CREATEHALL") == 0) {
hallName = strtok(NULL, " ");
createHall(&hallName);
}
...
}
void createHall(char **hallName) {
movie Movie;
strcpy(Movie.hallName, *hallName); // problem in here
...
}