Question: C program Why segmentation fault? Explain char * str; Printf(enter your input:) ; fgets(str, 20, stdin) ; Printf(%s, str) ; 2. Does this code fix

C program

Why segmentation fault? Explain

char * str; Printf("enter your input:") ; fgets(str, 20, stdin) ; Printf("%s", str) ;

2. Does this code fix it? Why or why not. If no, write your own code below

Void allocate_mem(char * str) { str = (char *) malloc (20* sizeof(char)) ; }

Int main(){

char * str; Printf("enter your input:") ; allocate_mem(str) ; fgets(str, 20, stdin) ; Printf("%s", str) ;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!