In myfunc() function, the pointer param is pointing to string pointer. It will look something like this:
---
param |-|--
--- |
|
|
---
string |-|----
--- |
|
|
--------------------------
|h|e|l|l|o|_|W|o|r|l|d|\0|
--------------------------
When you do param++, it will result in incrementing the pointer by the size of pointer1) on your platform because param can point to a char pointer. Assume that size of pointer is 8 (64 bit platform) and assume the address of string pointer is 100, then param++ will result in pointer param pointing to 108 address. Note that it will not affect string pointer in anyway. It will look something like this:
108
--- ---
param++ --> param |-|-------| |
--- ---
---
string |-|----
--- |
|
|
--------------------------
|h|e|l|l|o|_|W|o|r|l|d|\0|
--------------------------
Now, when you print the string in main() function, you are getting the output "hello_World".
To demonstrate this, I have added few printf() statement in your program:
#include <stdio.h>
void myfunc(char** param){
printf ("param : %p\n", (void *)param);
printf ("*param : %p\n", (void *)*param);
param++;
printf ("param : %p\n", (void *)param);
printf ("*param : %p\n", (void *)*param);
}
int main(void){
char* string = "hello_World";
printf ("string : %p\n", (void *)string);
myfunc(&string);
printf ("string : %p\n", (void *)string);
printf("%s\n", string);
return 0;
}
Output:
string : 0x10a654f97
param : 0x7ffee55abad0
*param : 0x10a654f97
param : 0x7ffee55abad8 ======> result of incrementing param
*param : 0xcb7d025 ======> result of incrementing param
string : 0x10a654f97 ======> not affected
hello_World
To get the desired behaviour, in the myfunc() function, you should first dereference the param pointer to get the string pointer and then increment it i.e. you should do (*param)++. It will result in increment the string pointer by size of a char1):
---
param |-|--
--- |
|
|
---
(*param)++ --> string |-|-------
--- |
|
|
--------------------------
|h|e|l|l|o|_|W|o|r|l|d|\0|
--------------------------
Demonstration:
#include <stdio.h>
void myfunc(char** param){
printf ("param : %p\n", (void *)param);
printf ("*param : %p\n", (void *)*param);
(*param)++;
printf ("param : %p\n", (void *)param);
printf ("*param : %p\n", (void *)*param);
}
int main(){
char* string = "hello_World";
printf ("string : %p\n", (void *)string);
myfunc(&string);
printf ("string : %p\n", (void *)string);
printf("%s\n", string);
return 0;
}
Output:
string : 0x100de5f97
param : 0x7ffeeee1aad0
*param : 0x100de5f97
param : 0x7ffeeee1aad0
*param : 0x100de5f98
string : 0x100de5f98
ello_World
- When increment a pointer, it gets incremented in steps of the object size that the pointer can point to.