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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-23-2005, 07:45 AM
|
#1
|
|
Member
Registered: Dec 2003
Posts: 72
Rep:
|
"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
|
|
|
|
03-23-2005, 08:13 AM
|
#2
|
|
Member
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388
Rep:
|
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).
|
|
|
|
03-23-2005, 08:49 AM
|
#3
|
|
Member
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349
Rep:
|
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.
|
03-23-2005, 08:57 AM
|
#4
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
03-23-2005, 09:27 AM
|
#5
|
|
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794
Rep: 
|
Are templates designed to solve the issue of same code for different types?
|
|
|
|
03-23-2005, 09:29 AM
|
#6
|
|
Member
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192
Rep:
|
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];
|
|
|
|
03-24-2005, 05:32 AM
|
#7
|
|
Member
Registered: Dec 2003
Posts: 72
Original Poster
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:39 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|