[Solved] Linked List pointers prob


There are several issues:

Instead of calling

 initialisation(&LBO);

which is not really wrong, just write:

LBO = NULL;

Then don’t hide pointers with typedefs, it only adds confusion.

Instead of:

typedef struct noeud
{
  int adresse, taille, temp;
  struct noeud* suivant;

} *liste;

Write:

struct noeud
{
  int adresse, taille, temp;
  struct noeud* suivant;    
};

and use struct noeud* instead of liste.

Now the real problem:

This is wrong. Here you allocate the size for a pointer, but you need to allocate the size for the whole structure:

q = malloc(sizeof(liste));

which is actually the same as:

q = malloc(sizeof(struct noeud*))

but you need:

q = malloc(sizeof(struct noeud))

You see now why hiding pointers with typedefs is a bad idea.

So here is the corrected version of your program (#includes ommitted for brevity):

struct noeud
{
  int adresse, taille, temp;
  struct noeud* suivant;
};

int random(int a, int b)
{
  return (a + (rand() % ((b + 1) + a)));
}

void creation(struct noeud** LBO)
{
  struct noeud* q, *prec = NULL;
  int i = 0;
  // srand(time(NULL));  <<<<< don't call srand here, call it once at the 
                            // beginning of the program
  while (i < 3)
  {
    printf("%d", i);
    q = malloc(sizeof(struct noeud));

    if (*LBO == NULL)
    {
      q->adresse = 0;
      q->taille = random(5, 45);
      q->temp = random(5, 15);
      q->suivant = *LBO;
      *LBO = q;
      i++;
    }
    else
    {
      prec = *LBO;
      q->taille = random(5, 45);
      q->temp = random(5, 15);
      q->adresse = prec->adresse + prec->taille;
      q->suivant = *LBO;
      *LBO = q;
      i++;
    }
  }
}

void affichage(struct noeud* LBO)
{
  printf("\nvoici ta struct noeud* \n ");
  while (LBO != NULL)
  {
    printf("%d-->", LBO->taille);
    LBO = LBO->suivant;
  }
  // if (LBO == NULL)  <<<<<<<<<<< drop this, LBO is always NULL here
                                // but it doesn't hurt, it's just useless
    printf("NULL");
}

int main()
{
  srand(time(NULL));   // <<<<<<<<<<<<< call srand here
  struct noeud* LBO;
  LBO = NULL;

  creation(&LBO);

  affichage(LBO);
  return 0;
}

There is still room for improvement, especially the creation function is somewhat awkward.

Also look at the comments with <<<<<<<<<<<, there are minor corrections

solved Linked List pointers prob