I know that a void pointer is useful for setting up generic functions etc. In any case, I also understand that a void pointer can be used to directly point to things like integers etc. I suspect that a void pointer can also be used to point to structure items as well; however, I can't seem to get the semantics related to a passing a void pointer into a structure. Specifically, i'm trying to change fcat.test from 200 to 300 by using the void pointer. No-matter, which permutation of casting i use, I get the following error "main.c:12:15: error: request for member ‘test’ in something not a structure or union." How do I go about accessing a structure via a void pointer and changing the values within it? My code is below.
#include <stdio.h>
typedef struct _cat{
int test;
}cat;
int main()
{
cat fcat;
fcat.test=200;
void * pointer=NULL;
pointer=&fcat; //This works
*((cat)pointer.test)=300; //I can't seem to figure out how to get 300 into the function via the void pointer. I've tried (cat *) as the typecast along with the -> operator after pointer. Nothing works.
}
((cat *)pointer)->test = 300-- just need to remember that prefix ops (like cast) are lower precedence than postfix (like ->)struct _catandcatare the same thing and interchangeable in all cases.