LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-23-2005, 07:45 AM   #1
f0rmula
Member
 
Registered: Dec 2003
Posts: 72

Rep: Reputation: 15
"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
 
Old 03-23-2005, 08:13 AM   #2
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
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).
 
Old 03-23-2005, 08:49 AM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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);
 
1 members found this post helpful.
Old 03-23-2005, 08:57 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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;
 
1 members found this post helpful.
Old 03-23-2005, 09:27 AM   #5
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Are templates designed to solve the issue of same code for different types?
 
Old 03-23-2005, 09:29 AM   #6
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
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];
 
Old 03-24-2005, 05:32 AM   #7
f0rmula
Member
 
Registered: Dec 2003
Posts: 72

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C error "dereferencing pointer to incomplete type" lucs Slackware 6 02-21-2005 09:33 AM
When "function pointer" meets "template"... I can't get rid of this compiling er cyu021 Linux - Software 3 12-17-2004 07:52 PM
Wine error: "glibc detected ... invlaid pointer" resaguk Slackware 2 11-08-2004 09:06 AM
curl "incompatible pointer type" while making PHP basse- Linux - Software 2 04-15-2004 10:21 AM
USB and PS/2 Mouse conflict - "jerky" pointer movements jonnyO2 Linux - Hardware 0 04-07-2004 05:05 AM


All times are GMT -5. The time now is 02:14 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration