S
subramanian100in
Will the following code always work ?
#include <stdio.h>
#include <stdlib.h>
# define SIZE 50
void myswap(char **name1, char **name2);
int main(void)
{
char *s1;
char *s2 ;
s1 = malloc(SIZE);
if (s1 == NULL)
{
printf("malloc failed\n");
exit(EXIT_FAILURE);
}
s2 = malloc(SIZE);
if (s2 == NULL)
{
free(s1);
s1 = NULL;
printf("malloc failed\n");
exit(EXIT_FAILURE);
}
printf("Before swapping: s1 = %p s2 = %p\n", (void *)s1, (void
*)s2);
myswap(&s1, &s2);
printf("After swapping: s1 = %p s2 = %p\n", (void *)s1, (void
*)s2);
free(s1);
s1 = NULL;
free(s2);
s2 = NULL;
return 0;
}
void myswap(char **name1, char **name2)
{
char *temp;
temp = *name1;
*name1=*name2;
*name2=temp;
}
#include <stdio.h>
#include <stdlib.h>
# define SIZE 50
void myswap(char **name1, char **name2);
int main(void)
{
char *s1;
char *s2 ;
s1 = malloc(SIZE);
if (s1 == NULL)
{
printf("malloc failed\n");
exit(EXIT_FAILURE);
}
s2 = malloc(SIZE);
if (s2 == NULL)
{
free(s1);
s1 = NULL;
printf("malloc failed\n");
exit(EXIT_FAILURE);
}
printf("Before swapping: s1 = %p s2 = %p\n", (void *)s1, (void
*)s2);
myswap(&s1, &s2);
printf("After swapping: s1 = %p s2 = %p\n", (void *)s1, (void
*)s2);
free(s1);
s1 = NULL;
free(s2);
s2 = NULL;
return 0;
}
void myswap(char **name1, char **name2)
{
char *temp;
temp = *name1;
*name1=*name2;
*name2=temp;
}