int main(){
...
//Here, other parts of the code are correctly written.
switch(n){
case 1:{
printf("Enter the path to image to be inserted: ");
char img[30];
scanf(" %[^\n]%*c", img);
add(&first, img); //Here is the func call
break;
}
...
return 0;
}
void add(movie **first, char *img){
movie *temp=(movie*)malloc(sizeof(movie));
strcpy(temp->img,img); // Debugger pointing the seg fault here
if(*first==NULL){
*first=temp;
temp->next=*first;
return;
}
movie *prev=*first;
while(prev->next!=*first){
prev=prev->next;
}
prev->next=temp;
temp->next=*first;
return;
}
This piece of code is causing segmentation fault. Debugger is pointing to strcpy line and throwing the following error:
Program received signal SIGSEGV, Segmentation fault.
__strcpy_sse2_unaligned ()
at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:546
546 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory.
Also the struct definition:
typedef struct Node{
char *img;
struct Node *next;
}movie;
Can anyone explain what is causing this segmentation fault?