I'm used to program in old 16bit C and 32bit x86 era . In old days we could use
void* type pointers to point to any arbitrary structure/string/class and we could easily de-reference any void* pointers to get any kind of data.
So previously we could use this type of code :
Code:
typedef struct {
int val;
long long lval;
void* next;
}mystr;
mystr first,second;
...
...
void* ptr = &first;
while(ptr)
{
printf("val = %d , lval = %lx\n",((mystr)(*ptr)).val,((mystr)(*ptr)).lval);
ptr =((mystr)(*ptr)).next;
}
I recently updated my distribution to x86-64, I don't know whether it is due to GCC 4 compiler or for 64bit compiler I can't use previous code. It throws this error :
Quote:
voidtest1.c:26: warning: dereferencing ‘void *’ pointer
voidtest1.c:26: error: void value not ignored as it ought to be
|
I tried many variation of casting , but no positive result.
Only way I could fix it by changing my code like this :
Code:
typedef struct {
int val;
long long lval;
void* next;
}mystr;
mystr first,second;
...
...
void* ptr = &first;
while(ptr)
{
mystr* tptr = (mystr*) ptr;
printf("val = %d , lval = %lx\n",(*tptr).val,(*tptr).lval);
ptr =(*tptr).next;
}
I'm asking this question as it is common in many current linux (x86-64) distribution (I have tested with Ubuntu 9.10 , OpenSuse 11.2 ]
This is more problematic with GTK library. In GTK , we are used to pass many arbitrary user supplied data via
gpointer . For example in signals , menubar and other places.
If anybody checks the declaration it is defined as below
Code:
#define gpointer void*
Now due to this change , we can't use gpointer properly in x86-64 linux and GCC 4.x .
Many old gtk programs fails to compile due to this type of
void* problem
Any ideas about how to properly use
void* in x86-64 version ?
and what we should avoid while using
void* ?
I am suggesting others to verify/experiment
void* related problem on your own and let me know the result.
Thanks in advance.