LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 01-23-2007, 04:27 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
convert void* to int


Hi,
i have found a c hashmap implementation and i am trying to write a program on it- i try to isolate the useful code snippets. The header file of the hashmap constains the following:

Code:
//hashmap.h
...
typedef void *any_t; //value of any type
extern map_t hashmap_new();
extern int hashmap_get(map_t in, int key, any_t *arg);
...
The way i want the hashmap to be used is to store an integer as a key AND an integer as the corresponding value. I have tried many things (that didn't work), but the last one is the following:

Code:
....
string dec2bin(int* num); //decimal to binary
int bin2dec(string* s);   //binary to decimal
...
any_t codeExtracted;
map_t cpress=hashmap_new();
string mixed;
...
if (hashmap_get(cpress,bin2dec(&mixed),&codeExtracted)!=MAP_MISSING)
{
	bfOut.PutBit((int)codeExtracted); //line 167
}
Here's the compile output:
lzw2.cpp:167: error: cast from ‘void*’ to ‘int’ loses precision

How can i deal with this?

Last edited by kpachopoulos; 01-23-2007 at 04:28 AM.
 
Old 01-23-2007, 04:46 AM   #2
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
if (hashmap_get(cpress,bin2dec(&mixed),&codeExtracted)!=MAP_MISSING)
{
bfOut.PutBit((int)codeExtracted); //line 167
}

i think the mistake is while u r passing "&codeExtracted"
try to type cast this as

if(hashmap_get(cpress,bin2dec(&mixed),(int *)&codeExtracted)!=MAP_MISSING)
{
bfOut.PutBit(codeExtracted); //line 167
}

no (int) in line 167
 
Old 01-23-2007, 04:59 AM   #3
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Original Poster
Rep: Reputation: 30
Problems/errors remain with both "(int *)&codeExtracted" and "bfOut.PutBit(codeExtracted);"
 
Old 01-23-2007, 05:21 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
codeEtracted is a pointer. You try to convert a pointer to an int and I suppose that that's not wat you want.

I always battle with this as well. Try
Code:
bfOut.PutBit(*(int*)codeExtracted)
or similar.

What probably will work is the use of an variable:
Code:
int *ptr;
...
...

ptr=(int*)codeExtracted;
bfOut.PutBit(*ptr)
Another mistake in your code might be
Code:
if (hashmap_get(cpress,bin2dec(&mixed),&codeExtracted)
codeExtracted is already a pointer, so I doubt that you have to use the '&'.
 
Old 01-23-2007, 05:42 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Code:
if (hashmap_get(cpress,bin2dec(&mixed),&codeExtracted)!=MAP_MISSING)
{
	bfOut.PutBit((int)codeExtracted); //line 167
}
Contrary to what others have said I think the pointer to pointer is probably correct and it's not unusual to pass a double pointer to fill with data(normally when memory is allocated in the function).

if put bit requires an int
Code:
	bfOut.PutBit((int)**codeExtracted);
if put bit requires a pointer to int
Code:
	bfOut.PutBit((int*)*codeExtracted);
[edit]You say this is based on C but is the above code C++ as this post would suggest?

Last edited by dmail; 01-23-2007 at 06:12 AM.
 
Old 01-23-2007, 11:13 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Contrary to what others have said I think the pointer to pointer is probably correct
You're right (I think) as any_t is also a pointer (so I was mistaken); sorry
 
Old 01-23-2007, 01:46 PM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
void * is safe to cast to any pointer type, or intptr_t. void * cannot be derefereced. Casting a void * to something other than the original pointer type and then dereferencing it is undefined.

Equavalent to dmail:

if put bit requires an int
Code:
	bfOut.PutBit(**(int **)codeExtracted);
if put bit requires a pointer to int
Code:
	bfOut.PutBit(*(int **)codeExtracted);

Last edited by tuxdev; 01-23-2007 at 01:49 PM.
 
Old 01-23-2007, 03:14 PM   #8
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
No mater, earlier or later there is a compiler version that takes that as an error even if you find a solution. May be the Object oriented layout of QT can help you.
void is nothing only a word with nothing behind (code, compilation) so void* is an error in its self - it is only a declaration. You only can handle pointers(all names) and values(that is behind a name) nothing else and void is not even NULL.
At least the linker has to solve all names especially extern(s) and globals.
What you also can handle is where a name1 beginns and where it ends by declaring a name2 behind that name1 and using name2 as end reference.
Explain more exactly what you want to do.
void says that the value of Register eax(32bit) rax(64bit) is invalid on return !!! nothing else. But you can put the value of eax in your int if you want - but that would only be the result of the last math operation or value of if().
 
Old 01-24-2007, 01:33 AM   #9
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
i am sorry!!!
i mis interpreted the 3rd argument of hashmap function
its a pointer to pointer

so i totally agree with what tuxdev has posted
 
Old 01-24-2007, 08:33 AM   #10
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by tuxdev
... void * cannot be derefereced...
Missed that
 
Old 01-24-2007, 11:56 AM   #11
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Original Poster
Rep: Reputation: 30
Thanks, everything is really useful.First of all, i forgot to mention, that the PutBit requires an int as an argument (PutBit(int i)).
@dmail: the hashmap implementation is in C.
"bfOut.PutBit(*(int **)codeExtracted)" seems to be right and the compiler doesn't complain. However, it will take me some time to test it...

Can somebody explain/give an example of what "void * cannot be derefereced" means? I know what references and pointers are, but i cannot clearly understand that... i'm not native english speaking
 
Old 01-24-2007, 04:03 PM   #12
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
I think our thread headline is a result of the error you get and totally dismatches your wish the thing should work.
Code:
typedef void *any_t;
Is a pointer to any function (reference). So the use of any_t garants that you don't get something on return. And I think that's not what you want. So take "void *" to the trash and type "int" instead. That would then also not be the right thing, but it makes you better understanding how it works.
You use:
Code:
if(hashmap_get(cpress,bin2dec(&mixed),&codeExtracted)!=MAP_MISSING)
but codeExtracted is yet a pointer, why you give in a pointer to a pointer - I can't believe that hashmap_get can handle that - I can't see what hashmap_get does. Because of that I wrote that above and my other message.
Are you really sure that you don't talk about java ?
 
Old 01-24-2007, 05:24 PM   #13
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Consider this code:
Code:
int foo;
void *bar = (void *)&foo;
*bar = 42;
The last line is dereferincing(* operator) a void pointer. There's probably a better explanation why it's wrong in K&R than I can spit out, go get a copy and read it cover-to-cover.

dmail: Thinking about it a bit more, I realized that dereferencing a void ** is legal, which may be what you were getting at.
 
Old 01-25-2007, 02:01 AM   #14
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by bastl
...
but codeExtracted is yet a pointer, why you give in a pointer to a pointer - I can't believe that hashmap_get can handle that ...
I think you have a point
 
  


Reply



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
Convert string (C++) to int FreeDoughnut Programming 10 10-30-2006 03:26 PM
typedef int fnct_ptr (void *) ArthurHuang Programming 1 08-21-2006 10:27 AM
int * function(void)??? kalleanka Programming 5 08-01-2006 09:37 AM
convert char * to int? calorie712 Programming 6 05-01-2006 04:38 AM
convert string to long/int alaios Programming 10 09-15-2005 09:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:13 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration