[Solved] SEGFAULT on strcpy


void findErrors(int lineIndex){
  char *line;
  strcpy(line, lines[lineIndex]);
//^ this is undefined behaviour.

At that point line is not initialized and strcopying something to a non initialized pointer results in undefined behaviour, usually a segfault.

But if by chance line points to some valid memory, the program may not crash and it may look as if it works. It’s undefined behaviour.

Case where it works:

void findErrors(int lineIndex){
  char line[255];
  strcpy(line, lines[lineIndex]);

Here line points to a buffer of length 255. You can strcpy to this buffer without problems as long as the length of the source string is smaller than 254.

Why “smaller than 254” and not 255 ?

Because we need one more char for the zero terminator.

solved SEGFAULT on strcpy