S
somenath
Hi All,
I was trying to understand the pass by value mechanism in c. To
understand it I wrote a
Small code as mentioned bellow.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void check(char *src);
int main(void)
{
char *s =malloc (10 * sizeof(*s));
strcpy(s,"hi");
printf("\n Value before function call = %s \n",s);
check(s);
printf("\n Value after function call = %s \n",s);
return 0;
}
void check(char *src)
{
free(src);
}
My assumption was it will print "hi " after call of check .
But it is printing
Value before function call = hi
Value after function call =
My understanding is "src" will receive a copy of "s" so if I free
"src" it will not effect "s".
Please let me know if my assumption is correct?
But for second thought
Here in "void check(char *src)" I am freeing memory
Location not obtain by malloc so it may be showing undefined
Behavior .
I am confused .Please provide some inputs.
Regards,
Somenath
I was trying to understand the pass by value mechanism in c. To
understand it I wrote a
Small code as mentioned bellow.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void check(char *src);
int main(void)
{
char *s =malloc (10 * sizeof(*s));
strcpy(s,"hi");
printf("\n Value before function call = %s \n",s);
check(s);
printf("\n Value after function call = %s \n",s);
return 0;
}
void check(char *src)
{
free(src);
}
My assumption was it will print "hi " after call of check .
But it is printing
Value before function call = hi
Value after function call =
My understanding is "src" will receive a copy of "s" so if I free
"src" it will not effect "s".
Please let me know if my assumption is correct?
But for second thought
Here in "void check(char *src)" I am freeing memory
Location not obtain by malloc so it may be showing undefined
Behavior .
I am confused .Please provide some inputs.
Regards,
Somenath