incompatible warning in C/C++

A

Ali

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

Axter

Ali said:
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
 
V

Victor Bazarov

Ali said:
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
 
V

Victor Bazarov

Axter said:
[..]
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
 
T

Tydr Schnubbis

Axter said:
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;

};
 
J

Jim Langston

Victor Bazarov said:
Ali said:
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?
 
D

deane_gavin

Jim said:
Victor Bazarov said:
Ali said:
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
 
E

Earl Purple

Victor said:
Ali said:
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.
 
N

Niklas Norrthon

Jim Langston said:
Victor Bazarov said:
Ali said:
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
 
V

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top