2

I got a problem at this line c->next=var;

and my function is :

{
    struct film
    {  
        int id;
        char nom[50];
        char typeFilm[50];
        int duree;
        int dateSortie;
        struct film *next;
    };

#include<stdio.h>
#include<string.h>
#include "structure_film.h"    
#include<stdlib.h>

void ajouter_film(struct film **h,struct film **c)
{
    struct film *var;
    int s, genre;
    do
    {
        var=(struct film*) malloc(sizeof(struct film));        
        printf("quel est le nom du film que vous voulez ajouter ? \n");
        scanf("%s",var->nom);  
        printf("\nquel est le type de ce film \n 1-action \n 2-aventure \n 3-romantique \n 4-comedie \n");
        scanf("%d",&genre);

        switch(genre)
        {
            case 1 :
                strcpy((var->typeFilm),"action");
                break;
            case 2 :
                strcpy((var->typeFilm),"aventure");
                break;
            case 3 :
                strcpy((var->typeFilm),"romantique");
                break;
            default :
                strcpy((var->typeFilm),"comedie");
        }
        printf("\nquel est sa duree ? \n");
        scanf("%d",&var->duree);
        printf("\nveuillez entrez le numero de reference de ce film : \n");
        scanf("%d",&var->id);        
        printf("\nquel est l'annee de sortie? \n");
        scanf("%d",&var->dateSortie);

        if (*h==NULL)
        {
            *h=var;
            *c=var;
        }
        else
        {
            printf("i'm in else \n");
            *c->next=var;
            *c=var;
            var->next=NULL;
        }
        printf("si vous voulez ajoutez un autre film veuillez tapez 1 si non tapez 0 \n");
        scanf("%d",&s);
    }
    while (s!=0);    
}
}
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
user3136456
  • 31
  • 1
  • 1
  • 5
  • 1
    Please properly format your source code, using the relevant buttons in the editor. It's looking messy – geert3 Dec 26 '13 at 11:06
  • possible duplicate of [compilation error: request for member in something not a structure or union](http://stackoverflow.com/questions/7384581/compilation-error-request-for-member-in-something-not-a-structure-or-union) and tons over others – alk Dec 26 '13 at 13:57

2 Answers2

7

The problem is the priority of the operands here:

*c->next=var;

This is evaluated as: "take the field next of the struct pointed by c, and then, deference it.". As c is not a pointer to struct film, the next field doesn't exist.

And you want: "deference the pointer c, and take the field next of the pointer that results from it." because *c is a pointer to struct film, and therefore, field next exists for it.

So, it's: (*c)->next=var; to alter the precedence order in the evaluation.

mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
1

This:

*c->next=var;

Probably needs to be this:

(*c)->next=var;

At least then it might compile--who knows if it's the behavior you want!

The reason is that c is a pointer to a pointer, so you need to dereference it twice to get the next field of the struct film.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436