Casting nonscalar to the same type.

C

crook

I have code below and it works properly but when I'm compiling it with
"--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids
casting nonscalar to the same type". How can I change this code to get
rid of this warning?

/*parameters is void* type*/
struct params p = ( struct params )*( ( struct params * )parameters );
 
R

Richard Heathfield

crook said:
I have code below and it works properly but when I'm compiling it with
"--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids
casting nonscalar to the same type". How can I change this code to get
rid of this warning?

/*parameters is void* type*/
struct params p = ( struct params )*( ( struct params * )parameters );

struct params *p = parameters;

Then just use p->whatever to gain access to the struct pointed to by p. If
you really must have a local copy, do this:

struct params localcopy = *p;

What is this fascination with casting? I just cannot fathom it. See
http://www.cpax.org.uk/prg/writings/casting.php for a fuller discussion of
casting, and why it's almost always a bad idea to do it.
 
B

Ben Pfaff

crook said:
I have code below and it works properly but when I'm compiling it with
"--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids
casting nonscalar to the same type". How can I change this code to get
rid of this warning?

/*parameters is void* type*/
struct params p = ( struct params )*( ( struct params * )parameters );

struct params p = *((struct params *) parameters);

By the way, I think all your extra spaces look funny, especially
around the unary "operator" *.
 
E

Eric Sosman

Richard Heathfield wrote On 06/29/06 16:35,:
crook said:




struct params *p = parameters;

Or perhaps

struct params p = *( (struct params * )parameters );

.... or even

struct params p;
memcpy(&p, parameters, sizeof p);

Can't really tell from a one-line snippet whether crook
wants `p' to be a struct or a pointer to a struct, nor
whether `parameters' points to a properly-aligned struct
instance or to a possibly mis-aligned "image."
 
K

Keith Thompson

crook said:
I have code below and it works properly but when I'm compiling it with
"--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids
casting nonscalar to the same type". How can I change this code to get
rid of this warning?

/*parameters is void* type*/
struct params p = ( struct params )*( ( struct params * )parameters );

The warning message is a bit misleading. ISO C doesn't just forbid
casting a nonscalar to the same type; it forbids casting a nonscalar
to or from *any* type. Both the operand of a cast operator, and the
type specified in the cast itself, must be of scalar type (either
arithmetic or pointer).

There's an exception to this, but it doesn't apply here. If the
target type is void, the operand can be of any type.
 
P

pete

Ben said:
struct params p = *((struct params *) parameters);

By the way, I think all your extra spaces look funny, especially
around the unary "operator" *.

Your example indicates that your defintition of
"all your extra spaces"
means extra white space beyond what you personally like to use.

I use the same extra white space around the assignment operator
that you do and the same extra white space preceding
the unary "operator" *, that you do,
but I don't usually follow a cast with white space.
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top