swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pointer question.


From: pauljohn
Subject: pointer question.
Date: Wed, 02 May 2001 19:20:46 -0500

I thought I was past all this, but now I'm humbled.  I want to calculate
new location coordinates and pass them back by reference. These methods
are in the same class, so I don't absolutely need the pointer usage, but
I really need to clear up my misunderstanding about pointers, so now I'm
asking.

x and y are IVARs with current location coordinates. xlag and ylag are
the "last time's" values.

Here's how the method gets called:

-chooseLocation
{
  int newx, newy;    

  newx=x; newy=y;

   //go to place with lower discontent than here
  double newDiscontent=[self  findExtremeType: safe X: &newx Y: &newy];
  if (newDiscontent < discontentHere)
            {
              xlag = x;
              ylag = y;
              x=newx;
              y=newy;
              agentMoved=YES;
             
            }
    return self;
}


And here's the top part of findExtremeType:X:Y:. I pass in pointers,
check to see if I'm on the edge, make the coordinates wrap around if I
am, and then printout some info.  By the printout, I can see that the
correct values are being calculated for xVals[] and yVals[], however,
something is not going right because *px and *py are not always
correctly dereferenced.


- (double) findExtremeType: (ExtremeType) type X: (int *) px Y: (int *)
py
{

  int i;
  double  bestValue;

  int offset = 0;
  int xpoints[] = {0,0,0,0,0,0,0,0};
  int ypoints[] = {0,0,0,0,0,0,0,0};
  int xVals[8] = {*px-1, *px-1,*px-1 , *px, *px,   *px+1, *px+1, *px+1};
  int yVals[8] = {*py-1, *py , *py+1, *py-1, *py+1, *py-1, *py, *py+1};
  int count = 0;

  if ( *px == 0 )
    {
     int correctx = worldXSize-1;
      xVals[0]= correctx;
      xVals[1]= correctx;
      xVals[2]= correctx;
    }
  
  if ( *px == worldXSize- 1 )
    {
      int correctx = 0;
      xVals[5]= correctx;
      xVals[6]= correctx;
      xVals[7]= correctx;
    }

  if ( *py == 0 )
    {
      int correcty = worldYSize-1;
      yVals[0]= correcty;
      yVals[3]= correcty;
      yVals[5]= correcty;
    }
  
  if ( *py == worldYSize- 1 )
    {
     int correcty = 0;
      yVals[2]= correcty;
      yVals[4]= correcty;
      yVals[7]= correcty;
    }

  if ( *px != x) raiseEvent(WarningMessage,"px not equal to x");


  if((*px=0) || (*py==0) ||(*px>worldXSize-1) || (*py>worldYSize-1)) {
    printf("*px is %d and it should be %d\n", *px, x);
    printf("*py is %d and it should be %d\n", *py, y);

  printf ("*px=%d,*py=%d x=%d,y=%d \n",*px,*py,x,y);
  for(i=0; i <8; i ++)
    {
      printf("%d %d,",xVals[i],yVals[i]);
    }
  printf("\n");
  }

-------snip snip -------

Now, in the console, I see this very puzzling info. The printout of
xVals and yVals are correct, but the displayed values of *px and *py are
not.  I BELIEVE that *px should be the same as x, and *py should be the
same as y. But, they are sometimes and  are not others.  THe
WarningMessage I ask for never appears, but the printfs tell a peculiar
story:


*px is 0 and it should be 9
*py is 0 and it should be 0
*px=0,*py=0 x=9,y=0
8 39,8 0,8 1,9 39,9 1,10 39,10 0,10 1,
*px is 0 and it should be 0
*py is 0 and it should be 0
*px=0,*py=0 x=0,y=0
39 39,39 0,39 1,0 39,0 1,1 39,1 0,1 1,
*px is 0 and it should be 28
*py is 0 and it should be 0
*px=0,*py=0 x=28,y=0
27 39,27 0,27 1,28 39,28 1,29 39,29 0,29 1,
*px is 0 and it should be 28
*py is 0 and it should be 0
*px=0,*py=0 x=28,y=0
27 39,27 0,27 1,28 39,28 1,29 39,29 0,29 1,
*px is 0 and it should be 0
*py is 0 and it should be 0
*px=0,*py=0 x=0,y=0
39 39,39 0,39 1,0 39,0 1,1 39,1 0,1 1,
*px is 0 and it should be 9
*py is 0 and it should be 0
*px=0,*py=0 x=9,y=0
8 39,8 0,8 1,9 39,9 1,10 39,10 0,10 1,

As I said, I can rewrite this without the pointers, but I want to
understand what is wrong.

-- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]