//why I cannot allocate memory
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void swap (char *str1, char *str2, char *a);
int main(void)
{
char *str1 = “The sky is blue”;
char *str2 = “And the land is green”;
char a[100] = {};
puts(“Before swapping”);
printf(“str1 = %sn”, str1);
printf(“str2 = %sn”, str2);
str1 = (char *)malloc((strlen(str1) + 100));
if (str1 == NULL)
puts(“alloc error”);
swap(str1, str2, a);
puts(“After swapping”);
printf(“str1 = %sn”, str1);
printf(“str2 = %sn”, str2);
free(str1);
return 0;
}
void swap (char *str1, char *str2, char *a)
{
strcpy(a, str2);
strcpy(str2, str1);
strcpy(str1, a);
}
Expert Answer
An answer will be send to you shortly. . . . .