Open In App

Pointer to Arrays in Objective-C

Last Updated : 12 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Objective-C, a pointer to an array is a way to store multiple values of the same data type in contiguous memory locations. These arrays can be manipulated and accessed using pointers, which are variables that store the memory address of another variable. In Objective-C, pointers to arrays are used to pass arrays as arguments to functions and to return arrays from functions. They are also used to dynamically allocate memory for arrays.

When working with arrays, it’s important to keep in mind that arrays are always passed by reference in Objective-C, meaning that any changes made to the array within a function will be reflected in the original array. To create a pointer to an array in Objective-C, you use the type of elements that the array will store, followed by an asterisk (*) and the name of the pointer. This is then used to store the memory address of the first element in the array.

Overall, understanding how to work with pointers to arrays in Objective-C is an important part of writing efficient and effective code in this programming language. It allows you to work with arrays in a flexible and dynamic way, making it possible to allocate memory for arrays at runtime and to pass arrays as arguments to functions.

Arrays can be declared and used in Several Ways

Here are the different types and subtypes of arrays in Objective-C, along with examples:

1. Static arrays: A static array is an array whose size is determined at the time of declaration and cannot be changed during runtime. For example:-  

int staticArray[5] = {1, 2, 3, 4, 5};

2. Dynamic arrays: A dynamic array is an array whose size can be changed during runtime by allocating memory dynamically using the malloc function. For example 

int *dynamicArray = (int *)malloc(5 * sizeof(int));

3. Two-dimensional arrays: A two-dimensional array is an array of arrays. It’s similar to a table with sections and divisions. For example:

int twoDimensionalArray[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

4.  Character arrays: Character arrays are arrays of characters and are used to store strings in Objective-C. For example:

char name[5] = “John”;

5. NSArray: NSArray is a class in Objective-C’s Foundation framework that provides an array-based data structure. NSArray objects can be created using a variety of initializers, including arrayWithObjects: , arrayWithArray:, and arrayWithContentsOfFile:. For example:

NSArray *fruits = [NSArray arrayWithObjects:@”Apple”, @”Banana”, @”Cherry”, nil];

6. NSMutableArray: NSMutableArray is a subclass of NSArray that provides the ability to add, remove, or modify elements in an array. For example:

NSMutableArray *colors = [NSMutableArray arrayWithObjects:@”Red”, @”Green”, @”Blue”, nil];

[colors addObject:@”Yellow”];

[colors removeObjectAtIndex:2];

Example 1:

This example defines a pointer to an array of int values and assigns it the address of an array of int values. It then prints the elements of the array using the pointer:

ObjectiveC




#import <Foundation/Foundation.h>
#include <stdio.h>
#include <stdlib.h>
  
int main(int argc, const char * argv[]) {
   @autoreleasepool {
       // Define an array of int values
       NSArray *array = @[@1, @2, @3, @4, @5];
         
       // Print the elements of the array
       for (int i = 0; i < [array count]; i++) {
           // NSLog is used to display the data at position i of the array.
           NSLog(@"%d\n", [array[i] intValue]);
       }
   }
   return EXIT_SUCCESS;
}


Output:

output

 

Example 2:

This example defines a function that takes a pointer to an array of int values and the size of the array as arguments, and returns the sum of the elements of the array:

ObjectiveC




#import <Foundation/Foundation.h>
#include <stdio.h>
#include <stdlib.h>
  
int main() {
   @autoreleasepool {
       // Define an array of int values
       NSArray *array = @[@1, @2, @3, @4, @5];
       // Initialize the sum to 0
       int sum = 0;
         
       // Loop through the elements of the array and add them to the sum
       for (int i = 0; i < [array count]; i++) {
           sum += [array[i] intValue];
       }
       NSLog(@"The sum of the elements of the array is %d", sum);
   }
   return EXIT_SUCCESS;
}


Output:

output

 

Example 3:

This example defines a pointer to an array of NSString objects and dynamically allocates an array of NSString objects at runtime:

ObjectiveC




#import <Foundation/Foundation.h>
  
int main() {
   @autoreleasepool {
       // Define an array of NSString objects
       NSMutableArray *stringArray = [NSMutableArray array];
         
       // Assign values to the elements of the array
       [stringArray addObject:@"One"];
       [stringArray addObject:@"Two"];
       [stringArray addObject:@"Three"];
       [stringArray addObject:@"Four"];
       [stringArray addObject:@"Five"];
         
       // Print the elements of the array
       for (NSString *string in stringArray) {
           NSLog(@"%@", string);
       }
   }
   return 0;
}


Output:

output

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads