Casting nonscalar to the same type.

Discussion in 'C Programming' started by crook, Jun 29, 2006.

  1. crook

    crook Guest

    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 );
     
    crook, Jun 29, 2006
    #1
    1. Advertising

  2. 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)
     
    Richard Heathfield, Jun 29, 2006
    #2
    1. Advertising

  3. crook

    Ben Pfaff Guest

    "crook" <> 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_
     
    Ben Pfaff, Jun 29, 2006
    #3
  4. crook

    Eric Sosman Guest

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

    --
     
    Eric Sosman, Jun 29, 2006
    #4
  5. "crook" <> 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) <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.
     
    Keith Thompson, Jun 29, 2006
    #5
  6. crook

    pete Guest

    Ben Pfaff wrote:
    >
    > "crook" <> 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
     
    pete, Jun 30, 2006
    #6
  7. crook

    crook Guest

    Thank you all for your explanations.
     
    crook, Jun 30, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,840
    Andrew McDonagh
    Jan 8, 2005
  2. heyo
    Replies:
    3
    Views:
    955
    Dan Pop
    Apr 1, 2004
  3. pete
    Replies:
    4
    Views:
    829
    Dan Pop
    Apr 2, 2004
  4. Jay Hamilton
    Replies:
    1
    Views:
    448
    Victor Bazarov
    Aug 11, 2006
  5. Wally Barnes
    Replies:
    3
    Views:
    545
    Wally Barnes
    Nov 20, 2008
Loading...

Share This Page