C Programming :: Dynamic Memory Allocation - Discussion
11 / 18
void myfunc(char** param){
++param;
}
int main(){
char* string = (char*)malloc(64);
strcpy(string, "hello_World");
myfunc(&string);
myfunc(&string);
printf("%s\n", string);
return 0;
}
void myfunc(char** param){
++param;
}
int main(){
char* string = (char*)malloc(64);
strcpy(string, "hello_World");
myfunc(&string);
myfunc(&string);
printf("%s\n", string);
return 0;
}
Ahello_World
Bello_World
Clo_World
Dllo_World
Show Explanation
in second statement we are calling myfunc where we are passing the address of string.
in function myfunc we are receiving in double pointer (**param).
i.e param will point to the address of string. if you print address of string
printf("%u\n",&string) and variable param like printf("%u\n", param), both address will be samein this function statement ++param will increment the address containing by param,
when you try to print string in main function it will print hello_world instead lo_World. if you change the statement in myfunc like ++(*param), it will print lo_World.
Asked In ::
In this program first we are allocating 64 byte dynamically and assigned to string.
in second statement we are calling myfunc where we are passing the address of string.
in function myfunc we are receiving in double pointer (**param).
i.e param will point to the address of string. if you print address of string
printf("%u\n",&string) and variable param like printf("%u\n", param), both address will be samein this function statement ++param will increment the address containing by param,
when you try to print string in main function it will print hello_world instead lo_World. if you change the statement in myfunc like ++(*param), it will print lo_World.
Read Full Answer
Report Error
Please Login First Click Here