By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

Casting nonscalar to the same type.

P: n/a
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 );

Jun 29 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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)
Jun 29 '06 #2

P: n/a
"crook" <ma*****@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_
Jun 29 '06 #3

P: n/a


Richard Heathfield wrote On 06/29/06 16:35,:
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;


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

--
Er*********@sun.com

Jun 29 '06 #4

P: n/a
"crook" <ma*****@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) ks***@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.
Jun 29 '06 #5

P: n/a
Ben Pfaff wrote:

"crook" <ma*****@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" *.


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
Jun 29 '06 #6

P: n/a
Thank you all for your explanations.

Jun 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.