Home > Blockchain >  SEGMENTATION_FAULT
SEGMENTATION_FAULT

Time:03-29

#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

CodePudding user response:

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];
  •  Tags:  
  • c
  • Related