   Error filling a linked struct list.
  Hello there. Im tryng to do a linked structure to store bitmaps, but im not making it right i guess, cause is crashing my program.Structs are declared global.struct no{  int num;  BITMAP *layer;  struct no *ante;};struct no *layers, *aux;Then, at Main, but not at the loop:    layers = (struct no *)malloc(sizeof(struct no));    aux = (struct no *)malloc(sizeof(struct no));    cria_layers();Then, the function that is giving me problem:void cria_layers(){ int tamanho; tamanho = m_tam + m_tam - 1; for(int z=0;z<tamanho;z++) {   if(layers==NULL)   {layers->num = z;layers->layer = create_bitmap(640, 480);layers->ante = NULL;   }   else   {aux = NULL;aux->num = z;aux->layer = create_bitmap(640, 480);aux->ante = layers;layers = aux;   } }}END_OF_FUNCTION(cria_layers);Whats wrong in this code?   
  What line crashes? What kind of crash?   ;  You're assigning aux to NULL then immediately dereferencing it. But beyond that, the code is confused ... what are you trying to do?   ;  Windryder is correct.  You are dereferencing a NULL pointer (well, 2 NULL pointers actually).These lines (the if/else) are basically the problem:if(layers==NULL){   layers->num = z;   layers->layer = create_bitmap(640, 480);   layers->ante = NULL;}else{   aux = NULL;   aux->num = z;   aux->layer = create_bitmap(640, 480);   aux->ante = layers;   layers = aux;}In the first case (in the if), layers is guaranteed to be NULL when you execute the code in the if block.  When you attempt to dereference the layers pointer to obtain the struct members (num, layer, ante), you are dereferencing a NULL pointer, so there is no actual struct there to get.The same basic situation occurs in the else block, except that aux is NULL instead of layers.My guess is that you want to change the conditional expression for the if to be layers != NULL instead of layers == NULL, and instead of assigning NULL to aux in the else block, you probably would want to make sure aux != NULL before executing the rest of that code.   ;Quote:Original post by needeepIn the first case (in the if), layers is guaranteed to be NULL when you execute the code in the if block.  Why would layers be NULL? He showed uslayers = (struct no *)malloc(sizeof(struct no));aux = (struct no *)malloc(sizeof(struct no));cria_layers();and then the definition of cria_layers(). layers will not be null in this call to cria_layers; the else clause will always fire and crash by dereferencing a null pointer. The if-clause would also deference a null pointer. Anyway, I think the OP is needs to work on his understanding of how pointers work; it's hard to offer advice because I can't tell from looking at this code what it's trying to do.   ;Quote:Original post by jwezorekWhy would layers be NULL? He showed us*code omitted*and then the definition of cria_layers(). layers will not be null in this call to cria_layers; the else clause will always fire and crash by dereferencing a null pointer. My apologies for not being clearer.  I am aware that when the code in main is executed (assuming malloc is successful) that the else-clause, not the if-clause, would be executed.  I was trying to point out the problems with both the if and else clauses.  My intent was saying that if the if-clause got executed, layers would be NULL.   ;  Let's see if we can make it a little clearer:Quote:Original post by MadHippievoid cria_layers(){ int tamanho; tamanho = m_tam + m_tam - 1; for(int z=0;z<tamanho;z++) {   if(layers==NULL)   {// I AM HERE BECAUSE 'layers' IS NULL// SO LET'S DO THINGS WITH 'layers'!layers->num = z;layers->layer = create_bitmap(640, 480);layers->ante = NULL;   }   else   {aux = NULL;// I JUST FINISHED MAKING 'aux' NULL// SO LET'S DO THINGS WITH 'aux'!aux->num = z;aux->layer = create_bitmap(640, 480);aux->ante = layers;layers = aux;   } }}END_OF_FUNCTION(cria_layers);