for(N = List->Head;N;N = N->Succ)

struct NODE{
    struct NODE Succ;
    struct NODE Prev;
};

struct LIST{
    struct NODE *Head;
    struct NODE *Tail;
    int count;
};

struct LIST *List;
struct NODE *N;

......
for(N = List->Head;N;N = N->Succ)
{
    ......
}
Compiler C51 changed value of List->Head because of the side effect of N = N->Succ.What can I do?

Parents
  • I have tried your code with some init statements and could not find out, that the list items ((Head/Tail) get changed after loop execution - the compiler works properly.

    Maybe you have some other additional code that changes the structure elements.

    struct NODE{
        struct NODE *Succ;
        struct NODE *Prev;
    };
    
    struct LIST{
        struct NODE *Head;
        struct NODE *Tail;
        int count;
    };
    
    
    struct LIST MyList;
    struct NODE FirstNode;
    struct NODE SecondNode;
    
    
    main()
    {
    	struct LIST *List;
    	struct NODE *N;
    
    	// init nodes and list
    	FirstNode.Succ = &SecondNode;
    	FirstNode.Prev = 0;
    	SecondNode.Succ = 0;
    	SecondNode.Prev = &FirstNode;
    	MyList.Head = &FirstNode;
    	MyList.Tail = &SecondNode;
    	MyList.count = 2;
    
    	List = &MyList;
    
    	// loop over all nodes
    	for(N = List->Head;N;N = N->Succ)
    	{
    	}
    
    }
    

Reply
  • I have tried your code with some init statements and could not find out, that the list items ((Head/Tail) get changed after loop execution - the compiler works properly.

    Maybe you have some other additional code that changes the structure elements.

    struct NODE{
        struct NODE *Succ;
        struct NODE *Prev;
    };
    
    struct LIST{
        struct NODE *Head;
        struct NODE *Tail;
        int count;
    };
    
    
    struct LIST MyList;
    struct NODE FirstNode;
    struct NODE SecondNode;
    
    
    main()
    {
    	struct LIST *List;
    	struct NODE *N;
    
    	// init nodes and list
    	FirstNode.Succ = &SecondNode;
    	FirstNode.Prev = 0;
    	SecondNode.Succ = 0;
    	SecondNode.Prev = &FirstNode;
    	MyList.Head = &FirstNode;
    	MyList.Tail = &SecondNode;
    	MyList.count = 2;
    
    	List = &MyList;
    
    	// loop over all nodes
    	for(N = List->Head;N;N = N->Succ)
    	{
    	}
    
    }
    

Children
More questions in this forum