Results 1 to 2 of 2

Thread: Not sure I understand this

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Not sure I understand this

    I have the following C sub:

    Code:
    void VFixPi(PCON pcon)
    {
      int co;
    	
      for(co = coWHITE; co <= coBLACK; co++) 
      {
    	int	ipiI;
    	int	ipiJ;
    
    	for(ipiI = ipiJ = 0; ipiI < pcon->argcpi[co]; ipiI++) 
    	{
    	  PPI ppiI = &pcon->argpi[co][ipiI];
    
    	  if(!ppiI->fDead) 
    	  {
    		PPI ppiJ = &pcon->argpi[co][ipiJ++];
    
    		*ppiJ = *ppiI;
    
    		pcon->argsq[ppiJ->isq].ppi = ppiJ;
    	  }
    	}
    	pcon->argcpi[co] = ipiJ;
      }
    }
    Note the code line *ppiJ = *ppiI;. These two always have the same address so it appears to me that the contents of the structure pointed to by ppiI is copied to the contents of the structure pointed to by ppiJ which is the same as copying the same data to the same location so I don't understand the point of doing this


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Not sure I understand this

    I assume you don't have any fDead flagged values.
    If fDead was set, you would skip the assignment, and ipiJ wouldn't be incremented.
    So, it seems the purpose is to remove dead values, compacting the array using i to index through all the fields, and copy to j which is incremented for non-dead fields.
    Only when you have a dead field will i increment while j doesn't so in the end all the non-dead fields will be packed to the front of pcon->argpi[co].
    If no fields are dead, then i will equal j the whole way. If you have some dead fields, i will lead j after that point.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width