---
title: "Multidimensional array of NSInteger as instance variable"  
description: "Multidimensional array of NSInteger as instance variable"  
author: "Tarun Kumar"  
published: 2015-07-16  
updated: 2015-07-16  
canonical: https://www.mindstick.com/forum/23344/multidimensional-array-of-nsinteger-as-instance-variable  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 2 minutes  

---

# Multidimensional array of NSInteger as instance variable

I'm attempting to understand [Objective C](https://www.mindstick.com/interview/22819/objective-c-vs-c-plus-plus) arrays. What I want to do is create a multidimensional NSInteger [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net).

Does the [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) immediately below create an array equivalent to initiating an array and running a double [for loop](https://www.mindstick.com/forum/12568/android-for-loop-not-working-in-main-thread) containing NSNull at each point?

```
@interface Builder: UIView {   NSInteger superDuperArray[5][4];}
```

How do I [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript)/[replace](https://yourviews.mindstick.com/view/81330/cow-antibody-research-in-usa-a-step-ahead-to-replace-plasma-therapy) specific [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) (NSIntegers) considering this is not an NSMutableArray? I need to do so elsewhere in the [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp), in several different methods.

## Replies

### Reply by Anonymous User

The instance variable you've declared does indeed create a multidimensional array that is accessible from any method in the object. It does not, however, contain NSNulls, because that's an object and NSInteger is a primitive type. It contains 0 in every slot (all ivars are initialized to their appropriate zero-value at the object's allocation).

If you don't need to change the size of the array, you're pretty much done. You access the array inside your object just like you'd access an array anywhere else, by subscripting: superDuperArray[2][1], e.g.

Here's a full sample:

```
#import< Foundation/Foundation.h> @interface Mutli : NSObject     - (NSInteger)numberAtRow:(NSInteger)row column:(NSInteger)col;    - (void)setNumber:(NSInteger)newNum atRow:(NSInteger)row column:(NSInteger)col;@end @implementation Mutli {    NSInteger numbers[5][4];}    -(void)setNumber:(NSInteger)newNum atRow:(NSInteger)row column:(NSInteger)col {               numbers[row][col] = newNum;    }     - (NSInteger)numberAtRow:(NSInteger)row column:(NSInteger)col {               return numbers[row][col];    }@end int main(int argc, const char * argv[]) {     @autoreleasepool {             Mutli * m = [Mutli new];              [m setNumber:10 atRow:2 column:1];             NSLog(@"%ld", [m numberAtRow:2 column:1]);              // This may not crash!            [m setNumber:10 atRow:100 column:34];     }     return 0;}
```

**N.B.** That last line. There's no bounds checking inherent to primitive arrays. Accesses outside the bounds you've set may not crash or cause any immediately-noticeable problem. Instead, you'll get garbage for reads, and will corrupt memory when you write. (Technically, you're invoking Undefined Behavior.) You should really include bounds checking in your accessor methods.

Also, a style note: it's no longer good ObjC form to put ivars in an object's interface. Hide it in the @implementation block as I've done.


---

Original Source: https://www.mindstick.com/forum/23344/multidimensional-array-of-nsinteger-as-instance-variable

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
