'SEGMENTATION_FAULT

#include <stdio.h>
#include <string.h>
int main()
{
  int take_photo = 0;
  char command[8];
  char * pictures = "take_pictures";

  strcpy(command, pictures);
  if (strcmp(command, "take_pictures") == 0) {
    printf("%s\n", "CLICK_PHOTO_TAKEN");
    take_photo = 1;
  } else {
    printf("%s\n", "SEG_FAULT_NO_PHOTO");
  }
  return 0;

}

Can someone tell me why I keep getting a SEGMENTATION FAULT error? It looks perfectly fine to me

Might have something to do with the array

c


Solution 1:[1]

You're getting the segfault at this line:

strcpy(command, pictures);

The string command is too small. You're trying to copy a long string to it. The behavior is undefined.

You can fix this by making command larger. For example,

char command[32];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aziz