LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "dereferencing `void *' pointer" but how else?!? (https://www.linuxquestions.org/questions/programming-9/dereferencing-%60void-%2A-pointer-but-how-else-305044/)

f0rmula 03-23-2005 07:45 AM

"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) {

  two_hex_chars = p[i];   
}

How can I get to the data pointed at by p without dereferencing this pointer? Do I HAVE to do memcpy?

I'd really appreciate any opinion on this.

James

trevelluk 03-23-2005 08:13 AM

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).

TheLinuxDuck 03-23-2005 08:49 AM

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);

itsme86 03-23-2005 08:57 AM

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;

Proud 03-23-2005 09:27 AM

Are templates designed to solve the issue of same code for different types?

nodger 03-23-2005 09:29 AM

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];

f0rmula 03-24-2005 05:32 AM

Quote:

Originally posted by trevelluk
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).

Spot on..

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.