Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply

C Programming - (void *) casting

 
Thread Tools Search this Thread
Old 06-30-2003, 03:25 PM   #1
James Leddy
 
Posts: n/a
Default (void *) casting

Hello,

I know you can cast a void pointer to an int, char, or any other type of
pointer, but can you do the reverse, cast an int or char pointer as a void
pounter?

This is the blowfish encipher algorithim and I need to make xl and xr void
because they come from a char * buffer, but I need them as unsigned longs.

void encipher_dword(void *xl, void *xr)
{
unsigned long *temp;
unsigned long *al, *ar;
int i;

al = (unsigned long *) xl;
ar = (unsigned long *) xr;
for (i = 0; i < N; i++) {
*al = *al ^ P[i];
*ar = f(al) ^ *ar;
SWAP(al, ar, temp);
}
SWAP(al, ar, temp);

*ar = *ar ^ P[N];
*al = *al ^ P[N + 1];
xl = (void *) al; //can I do this?
xr = ar; //should I do it like this?
}

I thought of another solution being to swap the actual values in al and ar,
insted of just the addresses, but I thought that would be slower
implementation.

Thanks,

--
-J. Leddy (a.k.a. iustitia)
  Reply With Quote
Old 07-01-2003, 12:13 AM   #2
Jack Klein
 
Posts: n/a
Default Re: (void *) casting

On Mon, 30 Jun 2003 15:25:14 GMT, James Leddy <>
wrote in comp.lang.c:

> Hello,
>
> I know you can cast a void pointer to an int, char, or any other type of
> pointer, but can you do the reverse, cast an int or char pointer as a void
> pounter?


If you are using C, no cast is necessary for pointers. You can assign
a pointer to void to a pointer to any other object type. You can
assign a pointer to any object type to a pointer to void. No cast at
all.

If your compiler requires a cast, you are using a C++ compiler.

On the other hand, casting a pointer type to a char or an int may
result in undefined behavior.

> This is the blowfish encipher algorithim and I need to make xl and xr void
> because they come from a char * buffer, but I need them as unsigned longs.
>
> void encipher_dword(void *xl, void *xr)
> {
> unsigned long *temp;
> unsigned long *al, *ar;
> int i;
>
> al = (unsigned long *) xl;
> ar = (unsigned long *) xr;


This assignment does not require a cast, unless you are using C++, in
which case you are posting in the wrong newsgroup.

But without seeing the code that calls this, there is no way of
telling if xl and xr are properly aligned to point to unsigned longs.
If not, the result is undefined behavior when you try to dereference
them. On some platforms the system will shut down your program with
some sort of bus error.

> for (i = 0; i < N; i++) {
> *al = *al ^ P[i];
> *ar = f(al) ^ *ar;
> SWAP(al, ar, temp);
> }
> SWAP(al, ar, temp);
>
> *ar = *ar ^ P[N];
> *al = *al ^ P[N + 1];
> xl = (void *) al; //can I do this?
> xr = ar; //should I do it like this?
> }
>
> I thought of another solution being to swap the actual values in al and ar,
> insted of just the addresses, but I thought that would be slower
> implementation.
>
> Thanks,


If you are concerned with speed, don't settle for what you thought.
Write a program and test it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
  Reply With Quote
Old 07-05-2003, 12:58 AM   #3
Martien Verbruggen
 
Posts: n/a
Default Re: (void *) casting

On Tue, 01 Jul 2003 00:13:20 GMT,
Jack Klein <> wrote:
> On Mon, 30 Jun 2003 15:25:14 GMT, James Leddy <>
> wrote in comp.lang.c:
>
>> Hello,
>>
>> I know you can cast a void pointer to an int, char, or any other type of
>> pointer, but can you do the reverse, cast an int or char pointer as a void
>> pounter?

>
> If you are using C, no cast is necessary for pointers. You can assign
> a pointer to void to a pointer to any other object type. You can
> assign a pointer to any object type to a pointer to void. No cast at
> all.


Not that this was relevant to the code the OP posted, but just for
completeness; there is (at least) one case where a cast to void * is
necessary (or at least generally more expedient than a temporary
variable of type void *):

char *p;
/* more code assigning something to p */
printf("Pointer p: %p\n", (void *) p);

Martien
--
|
Martien Verbruggen | Think of the average person. Half of the
| people out there are dumber.
|
  Reply With Quote
Old 07-05-2003, 09:36 PM   #4
Mike Wahler
 
Posts: n/a
Default Re: (void *) casting


Martien Verbruggen <> wrote in message
news:.. .
> On Tue, 01 Jul 2003 00:13:20 GMT,
> Jack Klein <> wrote:
> > On Mon, 30 Jun 2003 15:25:14 GMT, James Leddy <>
> > wrote in comp.lang.c:
> >
> >> Hello,
> >>
> >> I know you can cast a void pointer to an int, char, or any other type

of
> >> pointer, but can you do the reverse, cast an int or char pointer as a

void
> >> pounter?

> >
> > If you are using C, no cast is necessary for pointers. You can assign
> > a pointer to void to a pointer to any other object type. You can
> > assign a pointer to any object type to a pointer to void. No cast at
> > all.

>
> Not that this was relevant to the code the OP posted, but just for
> completeness; there is (at least) one case where a cast to void * is
> necessary (or at least generally more expedient than a temporary
> variable of type void *):
>
> char *p;
> /* more code assigning something to p */
> printf("Pointer p: %p\n", (void *) p);


Non-sequitur.

Your post does not address what Jack said:

"You can assign a pointer to void to a pointer to any
other object type. You can assign a pointer to any
object type to a pointer to void. No cast at all."

Your cast is not being used with an assignment.

-Mike


  Reply With Quote
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# arora.saurabh Software 0 05-31-2008 08:18 PM
Kiss Kiss Bang Bang Casting Call! FreddieMercury DVD Video 0 06-05-2006 05:28 PM
Kiss Kiss Bang Bang Casting Call! FreddieMercury DVD Video 0 06-05-2006 05:04 PM
Re: Favorite DVD - Documentary Ed Kim DVD Video 1 09-02-2004 03:10 PM
TheDigitalReview: TOUCHING THE VOID - DVD REVIEW Mike McGee DVD Video 0 06-09-2004 07:47 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47