There is code when compiling with the GCC compiler gives an error, when using the Visual C ++ compiler everything compiles without problems (the code is identical), how to fix this problem, I would not want to change the compiler and the current development environment (Codeblocks). I tried to create a new project helps. Mistake

#include <stdio.h> #include <stdlib.h> #define bool int static bool n; struct list { int field; // поле данных struct list *next; // указатель на следующий элемент struct list *prev; // указатель на предыдущий элемент }; struct list *init( int a ) // а- значение первого узла { struct list *lst; // выделение памяти под корень списка lst = ( struct list * )malloc( sizeof( struct list ) ); lst->field = a; lst->next = NULL; // указатель на следующий узел lst->prev = NULL; // указатель на предыдущий узел return( lst ); } struct list *addelem( list *lst, int number ) { struct list *temp, *p; temp = ( struct list * )malloc( sizeof( list ) ); p = lst->next; // сохранение указателя на следующий узел lst->next = temp; // предыдущий узел указывает на создаваемый temp->field = number; // сохранение поля данных добавляемого узла temp->next = p; // созданный узел указывает на следующий узел temp->prev = lst; // созданный узел указывает на предыдущий узел if( p != NULL ) { p->prev = temp; } return( temp ); } struct list *deletelem( list *lst ) { struct list *prev, *next; prev = lst->prev; // узел, предшествующий lst next = lst->next; // узел, следующий за lst if( prev != NULL ) { prev->next = lst->next; // переставляем указатель } if( next != NULL ) { next->prev = lst->prev; // переставляем указатель } free( lst ); // освобождаем память удаляемого элемента return( prev ); } struct list *deletehead( list *root ) { struct list *temp; temp = root->next; temp->prev = NULL; free( root ); // освобождение памяти текущего корня return( temp ); // новый корень списка } void findelem( list *lst, int goal ) { struct list *next; next = lst->next; if( lst->prev == NULL && lst->field == goal ) { n = 1; deletehead( lst ); } else if( lst->field == goal ) { deletelem( lst ); } else { findelem( next, goal ); } } void listprint( list *lst ) { struct list *p; p = lst; do { printf( "%d ", p->field ); // вывод значения элемента p p = p->next; // переход к следующему узлу } while( p != NULL ); // условие окончания обхода } void listprintr( list *lst ) { struct list *p; p = lst; while( p->next != NULL ) { p = p->next; // переход к концу списка } do { printf( "%d ", p->field ); // вывод значения элемента p p = p->next; // переход к следующему узлу } while( p != NULL ); // условие окончания обхода } int main() { list *head, *cur; int num; // Создаем список из 6 элементов printf( "a = " ); scanf( "%d", &num ); head = init( num ); cur = head; for( int i = 0; i < 5; i++ ) { printf( "a = " ); scanf( "%d", &num ); cur = addelem( cur, num ); } listprint( head ); printf( "\n" ); cur = head; int atr; printf( "Vvedit inform atrubyt: " ); scanf( "%d", &atr ); struct list *p2 = head->next; findelem( cur, atr ); if( n == 1 ) { listprint( p2 ); } else { listprint( head ); } printf( "\n" ); getchar(); getchar(); return 0; } 

    2 answers 2

    It seems that the MS VS compiler compiles your code as C ++ code, while GCC compiles your code as C code.

    The error is that before the name of the structure in C you must specify the keyword struct .

    For example, in this sentence

     struct list * addelem(list *lst, int number) { ^^^^^ 

    the C compiler will give an error message because it does not know what the name list means.

    Write in this sentence and in others where there is the same error, as follows

     struct list * addelem(struct list *lst, int number) { ^^^^^^^^^^^ 

    Another possibility to correct the error is to declare typedef_name for the structure. For example,

     typedef struct list { int field; // поле данных struct list *next; // указатель на следующий элемент struct list *prev; // указатель на предыдущий элемент } list; 

    And then you can use the list name without the struct keyword.

    • Used typedef, compiles without problems - andybelous2
    • @ andybelous2 As I wrote, the MS VS compiler compiled your code as C ++ code, so there was no error. :) - Vlad from Moscow

    If I understand correctly, the fact is that C ++, when struct foo ... creates a type foo , while C only creates a struct foo . If you are writing in C and not in C ++, then either put a struct list everywhere instead of a simple list , or typedef. If you are writing in C ++, then use the g ++ compiler instead of gcc.