(type)*(var1)-like casting

  • Thread starter grishin-mailing-lists
  • Start date
G

grishin-mailing-lists

Hi there,

Dann Corbit's coding encouraged me to dig deeper into it.
I preprocessed something and found this

typedef signed int Etype;
count[((((Etype)*(a + i)-(Etype)(-2147483647 -1)) >>
(((8)*((sizeof(Etype))))-((w)+1)*(8))) & ((1 << (8))-1)) + 1]++; (1)

It's a part of radix most significant digit sort
which I've been trying to understand.

Well, variable a is known as Etype*, allright.
Part of statement (1):
( (Etype)*(a + i) - (Etype) (-2147483647 -1) )

this is usual casting
(Etype) (-2147483647 -1)

but what is that
(Etype)*(a + i) ?

I thought it was a peculiarities of preprocessing and wrote a program
to test this:

#include <stdio.h>

int main(void)
{
/*TASK: to find out is it possible to implement casting like
(type *) var

this way
(type)*(var)

Is that correct syntax?
ANSWER: they aren't equal.
*/
int i;
int *p;
int z = 1;

p = &i;
z = (int)*(p);

printf("%p\n", p);
printf("%d\n", z);

z = (int)p;
printf("%d\n", z);

return 0;
}

I:\prj\_Unleashed_C\ch13>a
0022FF54
0
2293588

They are different!
Well, what is (Etype)*(a + i) for?
 
S

Seebs

but what is that
(Etype)*(a + i) ?

The same thing as any other
(type) expression
would be. It's a cast of <expression> to <type>.

So we obtain the value "*(a + i)" and convert that value to an
Etype.
They are different!
Well, what is (Etype)*(a + i) for?

You're getting confused because you're expecting it to be in some way
special, because of the odd visual similarity between the things on each
side of the *, and you're forgetting that * is a perfectly ordinary
unary operator. "*p" is "contents of pointer p". If a is a pointer,
and i is an integer, then "a+i" is a pointer, and "*(a+i)" is the contents
of that pointer.

-s
 
S

Seebs

Seebs said:
"*p" is "contents of pointer p". [...]

For sufficiently odd meanings of the word "contents".
*p is the object that p points to.

Yeah. That's what I was trying to say, but yours has the very slight
advantage of being clearly correct rather than at best very confusing, or
possibly totally wrong.

-s
 
L

lawrence.jones

Seebs said:
If a is a pointer,
and i is an integer, then "a+i" is a pointer, and "*(a+i)" is the contents
of that pointer.

It's worth pointing out that it's also a convoluted way of writing a.
I doubt the OP would have had any trouble at all understanding it if it
had been written as (Etype)a instead of (Etype)*(a + i).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top