c - void* type to char* type leadng -
the man has code
#include <conio.h> #include <stdio.h> void dbleint(void *a) { *((int*) a) *= 2; } void deleteevenint(void* a) { int tmp = *((int*) a); if (tmp % 2 == 0) { *((int*) a) = 0; } } void dbledouble(void *a) { *((double*) a) *= 2.0; } void deleteevendouble(void* a) { int tmp = *((double*) a); if (tmp % 2 == 0) { *((double*) a) = 0; } } // function takes array, size, size of 1 element , function //pointer, // applied elements of array void map(void *arr, unsigned num, size_t size, void (*fun)(void *)) { unsigned i; char *ptr = (char*) arr; (i = 0; < num; i++) { fun((void*) (ptr + i*size)); } }
why leads void* type char* type? can see thats error, when change code , dont lead it, why?
you're not allowed perform arithmetic on void pointers in c. following code illegal:
void *foo = whatever; foo + 1;
by casting foo
char *
, can perform arithmetic on pointer.
Comments
Post a Comment