incompatible warning in C/C++

Discussion in 'C++' started by Ali, Dec 20, 2005.

  1. Ali

    Ali Guest

    incompatible warning in C/C++

    I am using MS Visual C/C++ 6.0. I get the following warning when compiling

    C:model.ex.c(1221) : warning C4133: '=' : incompatible types - from
    'struct s_lsp_def *' to 'struct s_lsp_def *'


    In the .h file, I have
    ======================

    typedef struct s_lsp_def;

    typedef struct
    {
    int lsp_id;
    int priority;


    struct s_lsp_def* ptr_next_lsp_def;


    }s_lsp_def;

    in a .c file, model.ex.c, I have
    ================================

    int bcModel_add_lspToList ( s_lsp_def* the_ptr_lsp_def,
    {

    s_lsp_def* curr_ptr_lsp_def;

    // the following line is the PROBLEM

    curr_ptr_lsp_def->ptr_next_lsp_def = the_ptr_lsp_def;


    }


    Does anyone understand why this warning.
    Thanks.
     
    Ali, Dec 20, 2005
    #1
    1. Advertising

  2. Ali

    Axter Guest

    Ali wrote:
    > incompatible warning in C/C++
    >
    > I am using MS Visual C/C++ 6.0. I get the following warning when compiling
    >
    > C:model.ex.c(1221) : warning C4133: '=' : incompatible types - from
    > 'struct s_lsp_def *' to 'struct s_lsp_def *'
    >
    >
    > In the .h file, I have
    > ======================
    >
    > typedef struct s_lsp_def;
    >
    > typedef struct
    > {
    > int lsp_id;
    > int priority;
    >
    >
    > struct s_lsp_def* ptr_next_lsp_def;
    >
    >
    > }s_lsp_def;
    >
    > in a .c file, model.ex.c, I have
    > ================================
    >
    > int bcModel_add_lspToList ( s_lsp_def* the_ptr_lsp_def,
    > {
    >
    > s_lsp_def* curr_ptr_lsp_def;
    >
    > // the following line is the PROBLEM
    >
    > curr_ptr_lsp_def->ptr_next_lsp_def = the_ptr_lsp_def;
    >
    >
    > }
    >
    >
    > Does anyone understand why this warning.
    > Thanks.

    Try the following instead:
    typedef struct
    {
    int lsp_id;
    int priority;
    s_lsp_def* ptr_next_lsp_def;
    }s_lsp_def;

    You don't need the struct, since you're doing a typedef
     
    Axter, Dec 20, 2005
    #2
    1. Advertising

  3. Ali wrote:
    > incompatible warning in C/C++


    There is no such thing as "C/C++". It's either C or it is C++. Find out
    which and then post to the right newsgroup. If what you do is in fact C,
    post to comp.lang.c. Post here again if it is (unlikely) C++.

    > [...]


    V
     
    Victor Bazarov, Dec 20, 2005
    #3
  4. Axter wrote:
    > [..]
    > Try the following instead:
    > typedef struct
    > {
    > int lsp_id;
    > int priority;
    > s_lsp_def* ptr_next_lsp_def;
    > }s_lsp_def;
    >
    > You don't need the struct, since you're doing a typedef


    In C++ you don't need any typedef. You simply write

    struct s_lsp_def
    {
    ...
    s_lsp_def *ptr;
    };

    V
     
    Victor Bazarov, Dec 20, 2005
    #4
  5. Axter wrote:
    > Ali wrote:
    >> incompatible warning in C/C++
    >>
    >> I am using MS Visual C/C++ 6.0. I get the following warning when compiling
    >>
    >> C:model.ex.c(1221) : warning C4133: '=' : incompatible types - from
    >> 'struct s_lsp_def *' to 'struct s_lsp_def *'
    >>
    >>
    >> In the .h file, I have
    >> ======================
    >>
    >> typedef struct s_lsp_def;
    >>
    >> typedef struct
    >> {
    >> int lsp_id;
    >> int priority;
    >>
    >>
    >> struct s_lsp_def* ptr_next_lsp_def;
    >>
    >>
    >> }s_lsp_def;
    >>
    >> in a .c file, model.ex.c, I have
    >> ================================
    >>
    >> int bcModel_add_lspToList ( s_lsp_def* the_ptr_lsp_def,
    >> {
    >>
    >> s_lsp_def* curr_ptr_lsp_def;
    >>
    >> // the following line is the PROBLEM
    >>
    >> curr_ptr_lsp_def->ptr_next_lsp_def = the_ptr_lsp_def;
    >>
    >>
    >> }
    >>
    >>
    >> Does anyone understand why this warning.
    >> Thanks.

    > Try the following instead:
    > typedef struct
    > {
    > int lsp_id;
    > int priority;
    > s_lsp_def* ptr_next_lsp_def;
    > }s_lsp_def;
    >
    > You don't need the struct, since you're doing a typedef
    >

    Yes, but you can't use the typedef inside its own definition. But you
    can use the incomplete struct type itself. You just need to name it
    before using it, like so:

    typedef struct s_lsp_def
    {
    int lsp_id;
    int priority;

    struct s_lsp_def* ptr_next_lsp_def;

    } s_lsp_def;


    This is valid in both C and C++. In C++ you don't need the typedef,
    because a struct name works like a class name, so you can always use it
    without putting 'struct' in front of it.

    C++ only:

    struct s_lsp_def
    {
    int lsp_id;
    int priority;

    s_lsp_def* ptr_next_lsp_def;

    };
     
    Tydr Schnubbis, Dec 20, 2005
    #5
  6. Ali

    Jim Langston Guest

    "Victor Bazarov" <> wrote in message
    news:%%Zpf.58640$01.us.to.verio.net...
    > Ali wrote:
    >> incompatible warning in C/C++

    >
    > There is no such thing as "C/C++". It's either C or it is C++. Find out
    > which and then post to the right newsgroup. If what you do is in fact C,
    > post to comp.lang.c. Post here again if it is (unlikely) C++.
    >
    >> [...]

    >
    > V


    Actually, since c++ allows C code, I think your statement is a bit
    misleading Victor.

    I've seen "There is no such thing as "C/C++" too many times on this
    newsgroup and it is quite obvious what that the poster means "in C or
    C++...".

    Have you no concept of what / means?
     
    Jim Langston, Dec 21, 2005
    #6
  7. Ali

    Guest

    Jim Langston wrote:
    > "Victor Bazarov" <> wrote in message
    > news:%%Zpf.58640$01.us.to.verio.net...
    > > Ali wrote:
    > >> incompatible warning in C/C++

    > >
    > > There is no such thing as "C/C++". It's either C or it is C++. Find out
    > > which and then post to the right newsgroup. If what you do is in fact C,
    > > post to comp.lang.c. Post here again if it is (unlikely) C++.
    > >
    > >> [...]

    > >
    > > V

    >
    > Actually, since c++ allows C code, I think your statement is a bit
    > misleading Victor.
    >
    > I've seen "There is no such thing as "C/C++" too many times on this
    > newsgroup and it is quite obvious what that the poster means "in C or
    > C++...".
    >
    > Have you no concept of what / means?


    I have no concept of what "in C or in C++" means. They are different
    languages and solutions to the same problem in one language are often
    inappropriate or even incorrect in the other. So whether you say "in
    C/C++" or in "in C or in C++" you are probably doing so because you
    don't realise that, either way, it's a meaningless question.

    The use of "C/C++" seems to me to imply some muddled thinking along the
    lines of " I know they are kind of different but C++ is just a better C
    so they are basically the same thing". In which case, correcting that
    muddles thinking, as I think Victor did, is helpful.

    Gavin Deane
     
    , Dec 21, 2005
    #7
  8. Ali

    Earl Purple Guest

    Victor Bazarov wrote:
    > Ali wrote:
    > > incompatible warning in C/C++

    >
    > There is no such thing as "C/C++". It's either C or it is C++. Find out
    > which and then post to the right newsgroup. If what you do is in fact C,
    > post to comp.lang.c. Post here again if it is (unlikely) C++.
    >
    > > [...]

    >
    > V


    It is true there is no such language as C/C++ however you might want to
    write header files that are compatible with both languages, as both
    languages have the concept of a header file.

    > typedef struct s_lsp_def;


    Is that a valid forward declaration? typedef struct s_lsp_def to what?
    Presumably you want to say that s_lsp_def is a typedef to an unknown
    struct. I don't think that's allowed although it would be nice if it
    were.

    Now to answer the original problem, if you want to use the
    struct/typedef features that is used in C then you should give the
    struct a name, maybe s_lsp_def_tag. Then you can use that also within
    the struct itself thus:

    typedef struct s_lsp_def_tag
    {
    // code including a pointer to its own type
    } s_lsp_def;

    although I don't think you could use s_lsp_def for a forward
    declaration.
     
    Earl Purple, Dec 21, 2005
    #8
  9. "Jim Langston" <> writes:

    > "Victor Bazarov" <> wrote in message
    > news:%%Zpf.58640$01.us.to.verio.net...
    > > Ali wrote:
    > >> incompatible warning in C/C++

    > >
    > > There is no such thing as "C/C++". It's either C or it is C++. Find out
    > > which and then post to the right newsgroup. If what you do is in fact C,
    > > post to comp.lang.c. Post here again if it is (unlikely) C++.
    > >
    > >> [...]

    > >
    > > V

    >
    > Actually, since c++ allows C code, I think your statement is a bit
    > misleading Victor.


    I think the statement above is more than a bit misleading.

    A c++ compiler compiles C++ code. A c compiler compiles c code. C++
    does *not* compile C code.

    Try the following in your favorite c++ compiler:

    struct class
    {
    enum { zero, one, two, three } new;
    };

    int main(void)
    {
    struct class throw;
    return 0;
    }

    Or the following:

    #include <stdlib.h>

    int main(void)
    {
    int *x = malloc(1000 * sizeof *x);
    /* do something useful with x */
    free(x);
    return 0;
    }

    /Niklas Norrthon
     
    Niklas Norrthon, Dec 21, 2005
    #9
  10. Earl Purple wrote:
    > Victor Bazarov wrote:
    >> typedef struct s_lsp_def;


    I didn't write that.

    > Is that a valid forward declaration? typedef struct s_lsp_def to what?
    > Presumably you want to say that s_lsp_def is a typedef to an unknown
    > struct. I don't think that's allowed although it would be nice if it
    > were.


    You should simply write

    struct s_lsp_def;

    At least in C++ it's a valid forward-declaration of 's_lsp_def' as
    a struct (class).

    > [..]


    V
     
    Victor Bazarov, Dec 22, 2005
    #10
    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. Paminu
    Replies:
    7
    Views:
    2,430
    FlyingBird
    Feb 7, 2006
  2. Michael

    incompatible pointer type warning ?

    Michael, Jun 5, 2006, in forum: C Programming
    Replies:
    8
    Views:
    537
    Ian Collins
    Jun 5, 2006
  3. Replies:
    2
    Views:
    1,184
    Ian Collins
    Jul 26, 2006
  4. Replies:
    0
    Views:
    1,052
  5. Replies:
    1
    Views:
    513
    Andrew Poelstra
    Jul 26, 2006
Loading...

Share This Page