title: Strange values in an int array instance variable in Objective-C
body: <p>I looked for similar topics but no luck so far, so here it goes:</p>
  
  <p>In an Objective-C class I declared an <code>int</code> pointer instance variable to hold an  array of <code>int</code>:</p>
  
  <pre class="lang-c prettyprint-override"><code>@interface MyList : NSObject {
      int index;      // A simple int to hold an index reference
      NSString *name; // The name of the list
      int *bookList;  // A pointer to an int array that holds a list of numbers
  }
  
  @property (nonatomic) int index;
  @property (nonatomic, copy) NSString *name;
  @property (nonatomic) int *bookList;
  
  @end
  </code></pre>
  
  <p>I tested this as follows and <strong>all</strong> data contained in the instance variables was correctly stored and displayed by the <code>NSLog</code> statements:</p>
  
  <pre class="lang-c prettyprint-override"><code>MyList *aList = [[MyList alloc] init];
  [aList setIndex:1];
  [aList setName:@"ListOne"];
  [aList setBookList:(int []){1, 2, 3, 0}];
  
  NSLog(@"Show MyList object's data after object is populated");
  NSLog(@"[%d]: %@", aList.index, aList.name);
  for (int i = 0; i &lt; 4; i++) {
      NSLog(@"bookList[%d] = %d", i, aList.bookList[i]);
  }
  </code></pre>
  
  <p><em>However</em>, when I send this object as an argument to a method and I try to print the contents of the <code>int</code> array, I get strange numbers, and the same happens after returning from the method:</p>
  
  <pre class="lang-c prettyprint-override"><code>-(void)displayMyList:(MyList *)theList {
      NSLog(@"Show MyList object's data in displayMyList method");
      NSLog(@"[%d]: %@", theList.index, theList.name);
      for (int i = 0; i &lt; 4; i++) {
          NSLog(@"bookList[%d] = %d", i, theList.bookList[i]);
      }
  }
  </code></pre>
  
  <p>I don't know what's wrong with my test code, as the values of the <code>index</code> and <code>name</code> instance variables don't get changed when the object is sent to the <code>displayMyList:</code> method. I debugged step-by-step and the pointer to the <code>int</code> array points to the same address all the time, so it seems there's a side-effect somewhere that's changing the array's values, or I'm not getting how memory is placed for this type of pointers to <code>int</code> arrays. Maybe it's just some pointer arithmetic I'm not getting right, because I haven't seen this in any of the iPhone programming books I have.</p>
  
  <p>I wanted to implement the <code>int</code> array because the math I do with it is really simple and it seemed using an <code>NSArray</code> was overkill (if this approach doesn't work I can always go with an <code>NSArray</code>, though).</p>