typedef struct mystruct{
int a;
char arr[10];
char *str;
}mystruct;
void f(void *data, int offset){
char *s = (char *)(data + offset);
printf("%s", s);
}
void g(void *data, int offset){
char *s = *(char **)(data+offset);
printf("%s", s);
}
int main(){
mystruct test;
test.a = 2;
test.str = malloc(100);
strcpy(test.arr, "Hello ");
strcpy(test.str, "World!");
f(&test, offsetof(mystruct,arr));
g(&test, offsetof(mystruct,str));
return 0;
}
I am wondering why do I need two different ways to print strings. In function f, what is (data + offset) actually pointing at? Is it not pointing to arr which is a char pointer to the first element of the string? But in function g, (data + offset) is pointing to a char pointer too. So why two different approaches must be used to do the same task?