Insert void pointer into uint64_t array in C

I am currently working with the Linux kernel module, and I need to access some 64-bit values ​​stored in the array, however first I need to make void from the pointer.

I use a kernel function phys_to_virtthat returns a pointer to void, and I'm not quite sure how to actually use this void pointer to access the elements in the array that it points to.

I am currently doing this:

void *ptr;
uint64_t test;

ptr = phys_to_virt(physAddr);
test = *(uint64_t*)ptr;
printk("Test: %llx\n", test);

The value that I get from the test is not what I expected to see in the array, so I'm sure I'm doing something wrong. I need to access the first three elements in the array, so I need to point the void pointer to uint64_t [], but I'm not quite sure how to do this.

Any advice is appreciated.

thank

+5
1

phys_to_virt, void, , void , .

Yup, phys_to_virt() void *. void * , , - , , -, .

ptr = phys_to_virt(physAddr); // void * returned and saved to a void *, that fine

test = *(uint64_t*)ptr; // so: (uint64_t*)ptr is a typecast saying "ptr is now a 
                        //      uint64_t pointer", no issues there
                        // adding the "*" to the front deferences the pointer, and 
                        // deferencing a pointer (decayed from an array) gives you the
                        // first element of it.

, , test = *(uint64_t*)ptr; typecast . , :

test = ((uint64_t *)ptr)[0];

.

+3

All Articles