- Basic Objective-C
- Objective-C - Home
- Objective-C - Overview
- Objective-C - Environment Setup
- Objective-C - Program Structure
- Objective-C - Basic Syntax
- Objective-C - Data Types
- Objective-C - Variables
- Objective-C - Constants
- Objective-C - Operators
- Objective-C - Loops
- Objective-C - Decision Making
- Objective-C - Functions
- Objective-C - Blocks
- Objective-C - Numbers
- Objective-C - Arrays
- Objective-C - Pointers
- Objective-C - Strings
- Objective-C - Structures
- Objective-C - Preprocessors
- Objective-C - Typedef
- Objective-C - Type Casting
- Objective-C - Log Handling
- Objective-C - Error Handling
- Command-Line Arguments
- Advanced Objective-C
- Objective-C - Classes & Objects
- Objective-C - Inheritance
- Objective-C - Polymorphism
- Objective-C - Data Encapsulation
- Objective-C - Categories
- Objective-C - Posing
- Objective-C - Extensions
- Objective-C - Protocols
- Objective-C - Dynamic Binding
- Objective-C - Composite Objects
- Obj-C - Foundation Framework
- Objective-C - Fast Enumeration
- Obj-C - Memory Management
- Objective-C Useful Resources
- Objective-C - Quick Guide
- Objective-C - Useful Resources
- Objective-C - Discussion
Objective-C - Array of Pointers
Before we understand the concept of arrays of pointers, let us consider the following example, which makes use of an array of 3 integers −
#import <Foundation/Foundation.h> const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i; for (i = 0; i < MAX; i++) { NSLog(@"Value of var[%d] = %d\n", i, var[i] ); } return 0; }
When the above code is compiled and executed, it produces the following result −
2013-09-14 00:22:57.136 demo[18206] Value of var[0] = 10 2013-09-14 00:22:57.136 demo[18206] Value of var[1] = 100 2013-09-14 00:22:57.136 demo[18206] Value of var[2] = 200
There may be a situation when we want to maintain an array, which can store pointers to an int or char or any other data type available. Following is the declaration of an array of pointers to an integer −
int *ptr[MAX];
This declares ptr as an array of MAX integer pointers. Thus, each element in ptr now holds a pointer to an int value. Following example makes use of three integers, which will be stored in an array of pointers as follows −
#import <Foundation/Foundation.h> const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr[MAX]; for ( i = 0; i < MAX; i++) { ptr[i] = &var[i]; /* assign the address of integer. */ } for ( i = 0; i < MAX; i++) { NSLog(@"Value of var[%d] = %d\n", i, *ptr[i] ); } return 0; }
When the above code is compiled and executed, it produces the following result −
2013-09-14 00:27:47.904 demo[3825] Value of var[0] = 10 2013-09-14 00:27:47.905 demo[3825] Value of var[1] = 100 2013-09-14 00:27:47.905 demo[3825] Value of var[2] = 200
You can also use an array of pointers to character to store a list of strings as follows −
#import <Foundation/Foundation.h> const int MAX = 4; int main () { char *names[] = {"Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali",}; int i = 0; for ( i = 0; i < MAX; i++) { NSLog(@"Value of names[%d] = %s\n", i, names[i] ); } return 0; }
When the above code is compiled and executed, it produces the following result −
2013-09-14 00:30:25.438 demo[13476] Value of names[0] = Zara Ali 2013-09-14 00:30:25.439 demo[13476] Value of names[1] = Hina Ali 2013-09-14 00:30:25.439 demo[13476] Value of names[2] = Nuha Ali 2013-09-14 00:30:25.439 demo[13476] Value of names[3] = Sara Ali
To Continue Learning Please Login
Login with Google