0
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
    ...
}
B.IS
  • 41
  • 6
  • 5
    You have no context for the code. `hallName = strtok(NULL," ");` in isolation does nothing useful. Please post the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that shows the problem. – Weather Vane Oct 30 '16 at 12:13
  • So why are you suprised that running code which generated pointer warnings crashes? – Weather Vane Oct 30 '16 at 12:19
  • you don't look the return of strtok he could be NULL. `command = strtok(line," ");` here too `hallName = strtok(NULL," ");` and why send a `int **` in createHall. – Stargateur Oct 30 '16 at 12:36
  • 1
    Do not use `strncpy`: https://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/ – chqrlie Oct 30 '16 at 12:39
  • 1
    Syntax error: `char *hallName = NULL, char *command = NULL;` – chqrlie Oct 30 '16 at 12:41
  • 1
    Post **complete** code. Complete code includes a `main` function and allows readers to compile and run the code. If you're running it with command line arguments, tell us what. – Gilles 'SO- stop being evil' Oct 30 '16 at 12:42
  • add -if (hallName != NULL) control but still give segmentation fault. Actually no matter, i can send copy this. – B.IS Oct 30 '16 at 12:45
  • `hallName` is NULL, so you're taking the address of the pointer to char and pass it into `createHall`, with pointer not pointing to anything, ...did you actually debug this? You need some leg up on to read up on pointers first and foremost. – t0mm13b Oct 30 '16 at 12:51
  • @chqrlie I don't see a reference to `strncpy` in the OP's code. only `strcpy`. – t0mm13b Oct 30 '16 at 12:55
  • how do you call processCommand ? it is important the argument you pass is not in read-only memory since strtok writes to it – AndersK Oct 30 '16 at 13:16
  • `readLine()` is non standard. Does it allocate the array it returns a pointer to? If so, you should free it. – chqrlie Oct 30 '16 at 14:41

1 Answers1

1

There are multiple problems in your code:

  • you have a syntax error: char *hallName = NULL, char *command = NULL;.

  • you do not check the return values of strtok(): it can be NULL in case of mismatch.

  • you pass the address of the pointer hallName for no reason, just pass the value.

  • you do not check for potential overflow: if the hallName points to a long string, strcpy() will cause a buffer overrun.

  • you define movie as a typedef for struct Movie but also use Movie as the name for your local variable in createHall(). This is very confusing. Use Movie for both the struct tag and the typedef and lowercase movie for local variables.

Here is how to fix these problems:

#include <stdio.h>
#include <string.h>

typedef struct Movie {
    char hallName[50];
    char movieName[50];
} Movie;

...

void createHall(char *hallName) {
    Movie movie;
    snprintf(movie.hallName, sizeof movie.hallName, "%s", hallName);
    ...
    // do something with movie
}

void processCommand(char *line) {

    char *hallName = NULL;
    char *command = NULL;

    command = strtok(line, " ");

    if (command && strcmp(command, "CREATEHALL") == 0) {
        hallName = strtok(NULL, " ");
        if (hallName) {
            createHall(hallName);
        }
    }
}

Note that processCommand modified the array to which it receives a pointer. This is a bad API. You should try and avoid such side effects. strtok() is causing this side effect and it has other problems because it uses an internal state: avoid using this function.


You initially referred to strncpy in your question title, I edited that out since you do not actually use it. In fact you should never use strncpy(): its semantics are commonly misunderstood and very error prone. See this article by Bruce Dawson.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • are you sure that `movie.hallName` is a not a `char *` ? why not use strlen in place of sizeof ? – Stargateur Oct 30 '16 at 12:56
  • Where is it you're seeing `strncpy`? – t0mm13b Oct 30 '16 at 12:58
  • @t0mm13b look at [snprintf](https://linux.die.net/man/3/snprintf). very powerfull function. – Stargateur Oct 30 '16 at 13:00
  • @t0mm13b: the OP initially mentioned `strncpy` in the question title. He does not use and indeed should not. – chqrlie Oct 30 '16 at 13:02
  • 1
    @Stargateur: the definition of `Movie` is provided: `typedef struct Movie { char hallName[50]; char movieName[50]; } movie;`. It is quite confusing that the OP uses the same name for the typedef and the local variable. – chqrlie Oct 30 '16 at 13:03
  • @chqrlie Oh I didn't see. Never mind. – Stargateur Oct 30 '16 at 13:04
  • should have pointed it out earlier on, rather than leaving it too late, as the context about ‘strncpy‘ was lost through you editing the question. – t0mm13b Oct 30 '16 at 13:09
  • @chqrlie i tried this code snprintf instead of strcpy. I musnt snprintf because i have to compile -ansi. Program dont give segmentation fault but during call createHall function movieName dont go correctly in linux(in windows go correctly).For example movieName = "stardust" in process command before calling function but in createhall it is " " why ? – B.IS Oct 30 '16 at 13:39
  • *Ansi C* has been superseded 17 years ago! `snprintf` is available on most if not all systems. It is a shame to prevent newbies from using it! Can you post the actual calling code that shows the error? – chqrlie Oct 30 '16 at 13:44