Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain difference between the use of void pointer

My code needs to pass an array to a void pointer (The struct has (void *) that cannot be modified). The two versions of code below produce the same output but the latter has two warnings. My question is which of the two methods is preferred? Is there a way to typecast to remove the warnings?

This version does not have warnings and produces the output as expected:

#include <stdio.h>

void test(void *var_arr, char var_1);

typedef struct {
    char chip;
    void *buffer;
}test_struct;

int main()
{
   int test_array[3] = {3,7,5};
    char var_1 = 0x20;

    printf("Hello, World!\n");

    test(&test_array, var_1);
    return 0;
}

void test(void *var_arr, char var_1)
{
    int i;

    test_struct var_ts;

    var_ts.chip = var_1;
    var_ts.buffer = var_arr;

    for (i=0; i<3; ++i)
        printf("\nThe data values are : %X \n\r", *((int *)var_ts.buffer+i));

}

Hello, World!

The data values are : 3

The data values are : 7

The data values are : 5


This version below has two warnings but compiles and produces the output expected:

Warning(s): source_file.c: In function ‘main’: source_file.c:17:10: warning: passing argument 1 of ‘test’ from incompatible pointer type test(&test_array, var_1); ^ source_file.c:3:6: note: expected ‘int ’ but argument is of type ‘int ()[3]’ void test(int *var_arr, char var_1);

#include <stdio.h>

void test(int *var_arr, char var_1);

typedef struct {
    char chip;
    void *buffer;
}test_struct;

int main()
{
   int test_array[3] = {3,7,5};
    char var_1 = 0x20;

    printf("Hello, World!\n");

    test(&test_array, var_1);
    return 0;
}

void test(int *var_arr, char var_1)
{
    int i;

    test_struct var_ts;

    var_ts.chip = var_1;
    var_ts.buffer = (void *)var_arr;

    for (i=0; i<3; ++i)
        printf("\nThe data values are : %X \n\r", *((int *)var_ts.buffer+i));

}
like image 232
kcinicx Avatar asked Jan 27 '26 13:01

kcinicx


1 Answers

The lower version tells you what the problem was in the first one: passing a pointer to the array instead of a pointer to the first element.

Passing a pointer to the array, which has the type int(*)[3]:

test(&test_array, var_1);

Passing a pointer to the first element, which has the type int*:

test(test_array, var_1);   

The code happens to work because the two pointers points to the same address, so the pointer to the array appears to work, but the code is still undefined.

Passing a pointer to the first element is correct, as the array test_array, which has the type int[3] decays to type int* when it is passed to the function.

like image 188
2501 Avatar answered Jan 30 '26 05:01

2501