Your problem in both cases is in the struct definition: struct list *next doesn't refer to the struct you are in the process of declaring. Try this instead.
Your problem in both cases is in the struct definition: struct list *next doesn't refer to the struct you are in the process of declaring. Try this instead: typedef struct list { item_type item; struct list* next; } list; In addition, in C++ you must cast the void * returned by malloc to the appropriate pointer type (list *), C++ is stricter about these things. Also, BTW, in C++ you can leave off the typedef completely if you want.
The reason for the differing error messages is a difference in the languages. In C, the compiler knows that struct list * is a pointer to a struct, so it doesn't need to complain that it doesn't actually know what a "struct list" is yet. Later, though, when you try to assign this "struct list *" from a pointer of type "list *" (the type of which is "pointer to an anonymous struct"), it complains about the mismatch.In C++, a "struct" declaration is more or less equivalent to a "class" declaration (the major difference is in the default visibility of members).
Among other things, this means that structs in C++ are more or less automatically typedefed. So when the compiler sees "struct list *next", it takes it as a forward declaration of a class named "list"; then when it finishes the statement and processes the typedef, throws an error because you're trying to typedef something to an identifier that is already (forward-)declared as something else. Then it issues further errors because it doesn't actually know what "list" might be, due to the earlier error.
It doesn't have to have previous declaration, it interprets struct list *next as forward declaration. – Gene Bushuyev Aug 2 '11 at 17:10 @GeneBushuyev: Good point, I've adjusted the wording. – Anomie Aug 2 '11 at 17:22.
This is because c++ is stricter than c with respect to type conversions. There are host of other errors in your code. Please note that just putting a c source code, renaming the file as .
Cpp & compiling using g++ does not make a c source code as c++. If you are writing a program in c++ please use new & not malloc, doing so you do not need to explicitly type cast as in case of malloc.
– Als Aug 2 '11 at 17:05 1 This answer needs to be more specific: "There are host of other errors in your code" doesn't help anyone. – Chris Johnson Aug 2 '11 at 17:06 2 @Chris Johnson: By the time I finished writing the main erroneous part, other answers already came up with the elaborated list of errors, I don't think its worthwhile to repeat the same here, the answer, answered the basic flaw & the point I wanted to make. – Als Aug 2 '11 at 17:10.
C++ does not allow arbitrary pointer conversions, while C does. But since this is not considered good style, the compiler emits a warning. Just add a cast and it will solve both messages: p = (list*)malloc(sizeof(list)); Or if you want to be C++ only: p = new list; But then, you should declare constructors and such, also.
Casting the result of malloc() is considered poor style in C. It can mask errors; for example, if you forget the #include , some compilers will assume that malloc() returns an int. An idiom that avoids type mismatches is: p = malloc(sizeof *p);.
– Keith Thompson Aug 2 '11 at 18:21 And if you're trying to write code that will compile either as C or as C++, you're probably doing it wrong. C/C++ interoperability is good enough that there's rarely a good reason to do this. – Keith Thompson Aug 2 '11 at 18:22.
This is explained in this link. Quote: Gotcha for a C++ programmer using C Structs and Enums You have to include the struct keyword before the name of the struct type to declare a struct: In C++, you could do this struct a_struct { int x; }; a_struct struct_instance; and have a new instance of a_struct called struct_instance. In C, however, we have to include the struct keyword when declaring struct_instance: struct a_struct struct_instance; In fact, a similar situation also holds for declaring enums: in C, you must include the keyword enum; in C++, you don't have to.As a side note, most C programmers get around this issue by using typedefs: typedef struct struct_name { /* variables */ } struct_name_t; Now you can declare a struct with struct_name_t struct_name_t_instance; But there is another gotcha for C++ programmers: you must still use the "struct struct_name" syntax to declare a struct member that is a pointer to the struct.
Typedef struct struct_name { struct struct_name instance; struct_name_t instance2; /* invalid! The typedef isn't defined yet */ } struct_name_t.
You need to change this class: typedef struct{ item_type item; struct list* next; }list; to this: struct list { item_type item; list* next; }; Explanation: in the first example, you have anonymous structure, inside which struct list is forward declared. So when compiler sees typedef on the next line it finds a name collision, because typedef is not the same as struct declaration in C++.
Not quite; in list* next; "list" is undeclared. (C++ lets you refer to "struct list" as "list"; C doesn't. ) You want either struct list { item_type item; struct list *next; }; (and then refer to the type as "struct list") or typedef struct list { item_type item; struct list *next; } list;.
– Keith Thompson Aug 2 '11 at 18:24 @Keith -- list* next; is declared (compiler would have failed otherwise); inside class body all names (not only class name) are visible. What you probably wanted to say is that list is incomplete at that point, which is fine, it doesn't prevent compiler to take a pointer on incomplete type. – Gene Bushuyev Aug 2 '11 at 18:29 In C++, the declaration of struct list, even as an incomplete type, does let you refer to the type as list.In C, it doesn't -- which is why C code commonly uses typedefs for struct types.
(My own preference is to omit the typedef and just refer to the type as struct list. ) – Keith Thompson Aug 2 '11 at 19:28.
Since what you're doing is really defining a struct and then creating an alias with the typedef I think it's more readable to do this in the C case: typedef struct list_ { item_type item; struct list_* next; } list.
Since struct tags are in a different namespace (in the C sense of the term), it's perfectly legal to write typedef struct list { ... } list;; then "struct list" and "list" are two different names for the same type. – Keith Thompson Aug 2 '11 at 19:29 @Keith you're right, I guess I just prefer this style because it gives the impression that the user shouldn't make use of struct list_, but should go through the typedef, mainly for consistency. – mgalgs Aug 2 '11 at 20:27.
Use the following code #include #include #include using namespace std; typedef int item_type; struct list{ item_type item; list* next; }; void insert_list(list **l, item_type x){ list *p; p = (list*)malloc(sizeof(list)); p->item = x; p->next = *l; *l = p; } int main(){ return 0; }.
This is supposed to be C++ NOT C! – Als Aug 3 '11 at 4:53.
What C only warns against, C++ is likely to consider an error. It's a programming cultural thing. C was very forgiving in not enforcing it's typing system.
C++ is still quite forgiving, but you're doing something in C that even C++ won't forgive. When you malloc that block of memory, cast it to a pointer to a list. That will covert the address (pointer) to a pointer of the right type.
Without that cast, you could have malloc'd the size of anything, and there's no telling if it was meant to be referenced by a list pointer or some other pointer.
In C, casting the result of malloc() is more likely to create or conceal errors than to solve them. Just assign the result to your pointer object; the implicit conversion from void* to any pointer-to-object type will take care of the rest. Use p = malloc(sizeof *p); to ensure that all the sizes and types are consistent and correct.(This is specific to C; for C++, you probably shouldn't be using malloc() anyway.
) – Keith Thompson Aug 2 '11 at 18:26.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.