invalid operands to binary == or wrong type argument to unary exclamation mark

Discussion in 'C Programming' started by Sheldon, Nov 6, 2007.

  1. Sheldon

    Sheldon Guest

    Hi Everyone,

    I have defined a function:

    struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
    Transient retv);

    and in the code:

    struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
    Transient retv) {

    snip

    if(hdfdata != NULL) free(hdfdata);
    return retv;
    fail:
    if(hdfdata != NULL) free(hdfdata);
    exit(EXIT_FAILURE);
    }

    called the function like this:

    if (arrFromHdfNode(nodelist,retv) == NULL) {
    fprintf(stderr,"Failed getting data\n");
    goto fail;

    or like this

    if (!(arrFromHdfNode(nodelist,retv))) {
    fprintf(stderr,"Failed getting data\n");
    goto fail;

    The functions should return a struct or exit(EXIT_FAILURE) if
    something went wrong.

    For the first call I got the error:
    error: invalid operands to binary ==
    and for the second I got:
    error: wrong type argument to unary exclamation mark

    Can someone point me in the right direction? What is going on here?

    Thanks in advance,
    /S
     
    Sheldon, Nov 6, 2007
    #1
    1. Advertising

  2. Sheldon

    santosh Guest

    On Tuesday 06 Nov 2007 3:45 pm Sheldon <> wrote in
    article <>:

    > Hi Everyone,
    >
    > I have defined a function:
    >
    > struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
    > Transient retv);
    >
    > and in the code:
    >
    > struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
    > Transient retv) {
    >
    > snip
    >
    > if(hdfdata != NULL) free(hdfdata);
    > return retv;
    > fail:
    > if(hdfdata != NULL) free(hdfdata);
    > exit(EXIT_FAILURE);
    > }
    >
    > called the function like this:
    >
    > if (arrFromHdfNode(nodelist,retv) == NULL) {
    > fprintf(stderr,"Failed getting data\n");
    > goto fail;
    >
    > or like this
    >
    > if (!(arrFromHdfNode(nodelist,retv))) {
    > fprintf(stderr,"Failed getting data\n");
    > goto fail;
    >
    > The functions should return a struct or exit(EXIT_FAILURE) if
    > something went wrong.
    >
    > For the first call I got the error:
    > error: invalid operands to binary ==
    > and for the second I got:
    > error: wrong type argument to unary exclamation mark
    >
    > Can someone point me in the right direction? What is going on here?


    The function is defined as returning a struct Transient type object.
    NULL is a pointer constant value. Both are not directly comparable. One
    possibility is for the function to return a pointer to struct Transient
    and return a null pointer value on error.

    Similarly the ! operand accepts only a scalar type. A struct object is
    not a scalar type. Again returning a pointer to that struct type would
    solve this problem.
     
    santosh, Nov 6, 2007
    #2
    1. Advertising

  3. Sheldon

    Sheldon Guest

    On 6 Nov, 11:25, Richard Heathfield <> wrote:
    > Sheldon said:
    >
    >
    >
    >
    >
    > > Hi Everyone,

    >
    > > I have defined a function:

    >
    > > struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
    > > Transient retv);

    >
    > > and in the code:

    >
    > > struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
    > > Transient retv) {

    >
    > > snip

    >
    > > if(hdfdata != NULL) free(hdfdata);
    > > return retv;

    >
    > That's an unconditional return, and you're not in a loop or anything like
    > that. So everything from here to the end of that function is unreachable
    > code.
    >
    > > fail:

    >
    > You don't use this.
    >
    > > if(hdfdata != NULL) free(hdfdata);
    > > exit(EXIT_FAILURE);
    > > }

    >
    > > called the function like this:

    >
    > > if (arrFromHdfNode(nodelist,retv) == NULL) {

    >
    > But arrFromHdfNode returns a struct, not a pointer. C doesn't allow you to
    > use the == operator to compare structs, so you can't have a struct as one
    > of the operands, and the value returned by arrFromHdfNode(nodelist, retv)
    > has struct type, and that's why you're getting your error.
    >
    > > fprintf(stderr,"Failed getting data\n");
    > > goto fail;

    >
    > You're trying to use goto to jump from one function to a label in another
    > function. Again, this is against the rules of C.
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999- Dölj citerad text -
    >
    > - Visa citerad text -


    Thanks! Will redo the code accordingly.

    /S
     
    Sheldon, Nov 6, 2007
    #3
  4. Sheldon

    Sheldon Guest

    On 6 Nov, 11:22, santosh <> wrote:
    > On Tuesday 06 Nov 2007 3:45 pm Sheldon <> wrote in
    > article <>:
    >
    >
    >
    >
    >
    > > Hi Everyone,

    >
    > > I have defined a function:

    >
    > > struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
    > > Transient retv);

    >
    > > and in the code:

    >
    > > struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
    > > Transient retv) {

    >
    > > snip

    >
    > > if(hdfdata != NULL) free(hdfdata);
    > > return retv;
    > > fail:
    > > if(hdfdata != NULL) free(hdfdata);
    > > exit(EXIT_FAILURE);
    > > }

    >
    > > called the function like this:

    >
    > > if (arrFromHdfNode(nodelist,retv) == NULL) {
    > > fprintf(stderr,"Failed getting data\n");
    > > goto fail;

    >
    > > or like this

    >
    > > if (!(arrFromHdfNode(nodelist,retv))) {
    > > fprintf(stderr,"Failed getting data\n");
    > > goto fail;

    >
    > > The functions should return a struct or exit(EXIT_FAILURE) if
    > > something went wrong.

    >
    > > For the first call I got the error:
    > > error: invalid operands to binary ==
    > > and for the second I got:
    > > error: wrong type argument to unary exclamation mark

    >
    > > Can someone point me in the right direction? What is going on here?

    >
    > The function is defined as returning a struct Transient type object.
    > NULL is a pointer constant value. Both are not directly comparable. One
    > possibility is for the function to return a pointer to struct Transient
    > and return a null pointer value on error.
    >
    > Similarly the ! operand accepts only a scalar type. A struct object is
    > not a scalar type. Again returning a pointer to that struct type would
    > solve this problem.- Dölj citerad text -
    >
    > - Visa citerad text -


    Thanks! Will redo the code accordingly.

    /S
     
    Sheldon, Nov 6, 2007
    #4
  5. Sheldon said:

    > Hi Everyone,
    >
    > I have defined a function:
    >
    > struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
    > Transient retv);
    >
    > and in the code:
    >
    > struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
    > Transient retv) {
    >
    > snip
    >
    > if(hdfdata != NULL) free(hdfdata);
    > return retv;


    That's an unconditional return, and you're not in a loop or anything like
    that. So everything from here to the end of that function is unreachable
    code.

    > fail:


    You don't use this.

    > if(hdfdata != NULL) free(hdfdata);
    > exit(EXIT_FAILURE);
    > }
    >
    > called the function like this:
    >
    > if (arrFromHdfNode(nodelist,retv) == NULL) {


    But arrFromHdfNode returns a struct, not a pointer. C doesn't allow you to
    use the == operator to compare structs, so you can't have a struct as one
    of the operands, and the value returned by arrFromHdfNode(nodelist, retv)
    has struct type, and that's why you're getting your error.

    > fprintf(stderr,"Failed getting data\n");
    > goto fail;


    You're trying to use goto to jump from one function to a label in another
    function. Again, this is against the rules of C.


    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Nov 6, 2007
    #5
  6. Sheldon

    Willem Guest

    Richard wrote:
    ) Sheldon said:
    )> snip
    )>
    )> if(hdfdata != NULL) free(hdfdata);
    )> return retv;
    )
    ) That's an unconditional return, and you're not in a loop or anything like
    ) that. So everything from here to the end of that function is unreachable
    ) code.
    )
    )> fail:
    )
    ) You don't use this.

    Isn't that right there a label you can goto ?

    )> if(hdfdata != NULL) free(hdfdata);
    )> exit(EXIT_FAILURE);
    )> }


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Nov 6, 2007
    #6
    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. muthu

    error: invalid operands to binary &

    muthu, Jul 17, 2006, in forum: C Programming
    Replies:
    4
    Views:
    1,139
    Keith Thompson
    Jul 17, 2006
  2. Richard Eich

    [?] invalid operands to binary &

    Richard Eich, Feb 5, 2007, in forum: C Programming
    Replies:
    1
    Views:
    631
    Richard Eich
    Feb 5, 2007
  3. Spiros Bousbouras

    Invalid operands to binary -

    Spiros Bousbouras, Aug 8, 2007, in forum: C Programming
    Replies:
    11
    Views:
    1,858
    Martin Ambuhl
    Aug 8, 2007
  4. JoseMariaSola

    Is cast operator unary or binary? How many operands?

    JoseMariaSola, Apr 29, 2008, in forum: C Programming
    Replies:
    16
    Views:
    1,188
    David Thompson
    May 12, 2008
  5. Guy
    Replies:
    4
    Views:
    186
    David Dorward
    Dec 13, 2003
Loading...

Share This Page