For Programmers: Free Programming Magazines  


Home > Archive > C > June 2006 > Casting nonscalar to the same type.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Casting nonscalar to the same type.
crook

2006-06-29, 6:56 pm

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 );

Richard Heathfield

2006-06-29, 6:56 pm

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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Ben Pfaff

2006-06-29, 6:56 pm

"crook" <mateusz@bsdmail.org> writes:

> 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" *.
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Eric Sosman

2006-06-29, 6:56 pm



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."

--
Eric.Sosman@sun.com

Keith Thompson

2006-06-29, 6:56 pm

"crook" <mateusz@bsdmail.org> writes:
> 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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
pete

2006-06-29, 6:56 pm

Ben Pfaff wrote:
>
> "crook" <mateusz@bsdmail.org> writes:
>
>
> 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.

--
pete
crook

2006-06-30, 3:56 am

Thank you all for your explanations.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com