"dereferencing `void *' pointer" but how else?!?
Gah.. I keep getting the following when I try to compile this code..
dstr.c:356: warning: dereferencing `void *' pointer dstr.c:356: error: void value not ignored as it ought to be Code:
void a_function(const void* p) { I'd really appreciate any opinion on this. James |
Well, I think the problem is your use of p[i], which (as you probably know), means the i'th element of array p. But because p is a void pointer, the compiler doesn't know where this element is. Each element of p could just be a single byte, or could be a structure of hundreds of bytes.
Is there some reason why you're using a void *? My advice would be to specify suitable type for this pointer (which should be the same type as two_hex_chars). |
Firstly, why is p void? I'm willing to bet that whatever variable type the code is passing in is not void. Second, what type of var is two_hex_chars? Third, what is i? Where is it defined?
Not knowing what all these variables represent, I can only say that you'll need to typecast p, prior to it's dereferencing, to the type that two_hex_chars is. If I assume that two_hex_chars is a char, then I can simply say: Code:
two_hex_chars = *((char *)p + i); |
He's probably writing a function that can handle a pointer to any type of data which is what a void pointer is for. i'm not sure where the index [/b]i[/b] is declared, but you can get just the first byte that p points to like this:
Code:
two_hex_chars = *(char *)p; |
Are templates designed to solve the issue of same code for different types?
|
Yup, just cast in to a char* and the compiler will know how many bytes to jump for each increment of i
Code:
two_hex_chars = ((char*)p)[i]; |
Quote:
What is the increment of a (void *)? who knows.. I'm just looking through the binary data converting it to hex, but I'm doing it one byte at a time.. Therefore, I've fixed it by casting it to a (char *), and can now repeatedly increment the char* pointer.. Concice and accurate reply.. cheers :) James |
All times are GMT -5. The time now is 09:55 PM. |