The following segment of code is generating a segmentation f

The following segment of code is generating a segmentation fault when it executes. Where is the segmentation fault occurring? Why is the segmentation fault occurring? How would you fix the problem? char * combineStrings (char*stringl, char * s t r i n g 2) {char * returnString, if ((s t r i n g 1 ! = NULL) && (s t r i n g 2! =NULL)) {strcpy(returnString. string 1); strcat(returnString, string2), } returnreturnString, }

Solution

1)at the comarision the problem is occuring

2) if sufficient memory is not allocated at the runtime for the resultant variable segmentation problem will occur

3)I just allocated the memory at runtime

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system(\"pause\") or input loop */
char *combineStrings(char* string1,char *string2){
   char* returnString;
   if((string1!=NULL)&&(string2!=NULL)){
       returnString = malloc(strlen(string1)+1+4);
       strcpy(returnString,string1);   
       strcat(returnString,string2);
   }
   return returnString;
}
int main(int argc, char *argv[]) {
   printf(\"String Operations:\ \");
   char* string1 = \"Your \";
   char* string2= \"Friend\";
   char* result =combineStrings(string1,string2);
   printf(\"%s\",result);
   return 0;
}

 The following segment of code is generating a segmentation fault when it executes. Where is the segmentation fault occurring? Why is the segmentation fault occ

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site