Search code examples
c

how to solve the error (expected identifier before'(' token) in C


its my first question here.. In the following code. It keeps giving me the following error in the scanf lines: Expected identifier before '(' token. i dont know how to solve it..

typedef struct avion
{
    int code;
    int capacite;
    char etat[1];
    int date;
    int nvols;
} avion;
typedef struct element *list;
typedef struct element
{
    avion A;
    struct element *svt;
} element;
        
list *Modifier(list* av)   
    {
        list *p=av;
        int c;
        printf("\n------------La Modification--------------\n");
        printf("\nDonner le code de l'avion que vous voulez modifier... :");
        scanf("%d",&c);
        while(av!=NULL) 
        {
            if ((av->(A.code)) == c)
            {   
            printf("\nL'avion existe dans la liste...");
            printf("\nDonner le nouveau code:");
            scanf("%d",&av->(A.code));
            printf("\nDonner la nouvelle capacite...:");
            scanf("%d",av->(A.capacite));
            printf("\nDonner le nouveau etat...:");
            scanf("%s",av->(A.etat));
            printf("\nDonner la nouvelle date de fabrication...:");
            scanf("%d",av->(A.date));
            printf("\nDonner le nouveau nombre de vols assures...:");
            scanf("%d",(av->(A.nvols)));
            return p;
            }
            (*p)=p ->(svt);
        }
     }

Solution

  • The "expected identifier before '(' token" error occurs because you are using -> operator to access a field of a struct and, instead of passing the field identifier, you are passing a '(' character.

    Here is the list of errors.

    1. av->(A.code) is bad syntax. If av is a pointer to a struct that contains a struct field called A and you want to access the field code of A, use av->A.code;
    2. (*p) = p->(svt) is also bad syntax. If you want p to point to the next element of the list, witch I assumed is pointed by the field svt, use p = p->svt;
    3. You forgot to pass the address of your integers when you use scanf() with "%d" identifier, inserting the '&' character before the variable names;
    4. Replace typedef struct element *list for typedef struct element list;
    5. Note that the function will return after the first element on whose code field equals c. If you meant to do that, you need to return a list * after the while loop. I suggest you to return NULL to signalize no element of the list was modified.

    Here is my suggestion.

    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct avion {
        int code;
        int capacite;
        char etat[1];
        int date;
        int nvols;
    } avion;
    
    typedef struct element {
        avion A;
        struct element *svt;
    } element;
    
    typedef struct element list;
    
    list *modifier(list* av) {
        list *p=av;
        int c;
        printf("\n------------La Modification--------------\n");
        printf("\nDonner le code de l'avion que vous voulez modifier...: ");
        scanf("%d", &c);
        while (av!=NULL) {
            if (av->A.code == c) {   
                printf("\nL'avion existe dans la liste...");
                printf("\nDonner le nouveau code: ");
                scanf("%d", &av->A.code);
                printf("\nDonner la nouvelle capacite...: ");
                scanf("%d", &av->A.capacite);
                printf("\nDonner le nouveau etat...: ");
                scanf("%s", av->A.etat);
                printf("\nDonner la nouvelle date de fabrication...: ");
                scanf("%d", &av->A.date);
                printf("\nDonner le nouveau nombre de vols assures...: ");
                scanf("%d", &av->A.nvols);
                return p;
            }
            p = p->svt;
        }
        return NULL;
     }