Troubles with void pointers

Hello Guys, i have this code down here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void test_func(void *vp1, void *vp2);

int main(int argc, char const *argv[])
{
    int size = 2;
    
    char **_char_arr = (char **)malloc(size * sizeof(char *));
    _char_arr[0] = (char*)malloc(100 * sizeof(char));
    _char_arr[1] = (char*)malloc(100 * sizeof(char));
    *(_char_arr + 0) = "hello\0";
    *(_char_arr + 1) = "world\0";

    
    printf("arr1 1: %p, arr1 value: %s\n", _char_arr, _char_arr[0]);
    printf("arr2 2: %p, arr2 value: %s\n", _char_arr + 1, _char_arr[1]);
    test_func(_char_arr, _char_arr + 1);
    
    free(_char_arr);
    
    return 0;
}

void test_func(void *vp1, void *vp2)
{
    
    printf("vp 1: %p, vp1 value%s\n", vp1, (char *)vp1);
    printf("vp 2: %p, vp2 value%s\n", vp2, (char *)vp2);
}


//////////////////
Here is the output

arr1 1: 0x100200040, arr1 value: hello
arr2 2: 0x100200048, arr2 value: world
vp 1: 0x100200040, vp1 value\251
vp 2: 0x100200048, vp2 value\260
Program ended with exit code: 0

//////////////////
I'm just passing the 2 array to the "test_func" and, as can you see, the pointers of two array is the same, out and inside the function.. but i cant print the value.. inside the function.
Even if i explicit cast to a char pointer.. what is the issue here?
Thank you all for the answare.
*(_char_arr + 0) = "hello\0";

This doesn't copy the characters 'h' 'e' 'l' 'l' 'o' into anything, but I suspect you may be causing a memory leak doing it.

Is there a reason why you're doing C code manual memory management? If you were doing this in C++, it would be something like this:

1
2
vector<string> strings {"hello", "world"};
cout << strings[0] << '\n' << strings[1];
Last edited on
L16 needs to dereference the args - as they are pointer to pointer

 
test_func(*_char_arr, *(_char_arr + 1));


or

 
test_func(_char_arr[0], _char_arr[1]);


Also note that because of L10, 11 you don't need L8, 9

But if you have L8, 9 then you also need to free the allocated memory. Just freeing _char_arr doesn't also free the separately allocated memory in L8, 9.

Last edited on
Change the printf in test_func to:
1
2
3
4
5
6
void test_func(void *vp1, void *vp2)
{
    
    printf("vp 1: %p, vp1 value%s\n", vp1, ((char **)vp1)[0]);
    printf("vp 2: %p, vp2 value%s\n", vp2, ((char **)vp2)[0]);
}
Oh wow.. thank you all guys, you always save my life.

I have a question about this line: printf("vp 1: %p, vp1 value%s\n", vp1, ((char **)vp1)[0]);.

is there a way to write this: ((char **)vp2)[0] with aritmetic pointer? without square brackets?

Thank you guys!!
Last edited on
For an array, (expression)[x] is equivalent to *((expression) + x). Can you work backwards to see what to do for ((char **)vp2)[0]?

By the way, please note that in real code outside of an academic setting, the arr[i] syntax is almost universally preferred over the *(arr + i) syntax to express intent better when using arrays.
Oh ok, i didn't know about this "note that in real code outside of an academic setting, the arr[i] syntax is almost universally preferred"

Ok i will work on this..

Thank you all guys.
Have a good day :D
Topic archived. No new replies allowed.