Open In App

Passing Pointers to Functions in Objective-C

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

Pointers are a crucial aspect of any programming language and passing pointers to functions in Objective-C is no exception. In Objective-C, pointers are used to pass values between functions and to manipulate memory in a more flexible manner. Pointers can be passed as arguments to functions and can also be returned as function results.

Passing pointers to functions in Objective-C can help you in optimizing the memory utilization of your code, reduce the number of function calls, and improve the overall performance of your code. In this article, we will discuss how to pass pointers to functions in Objective-C, understand the different types of pointer arguments, and see some examples to get a better understanding of the concept.

Types and Subtypes of Pointers

Objective-C, pointers can be of two types: constant pointers and non-constant pointers.

  1. Constant Pointers: When a pointer is declared as a constant, its value cannot be changed.
  2. Non-Constant Pointers: When a pointer is not declared as a constant, its value can be changed.

Similarly, pointers can also be of two subtypes: input pointers and output pointers.

  1. Input Pointers: Input pointers are used to pass values into a function.
  2. Output Pointers: Output pointers are used to return values from a function.

Syntax:

The syntax for passing pointers to functions in Objective-C is as follows:

-(void) sampleFunction:(int *) inputPointer;
-(int *) sampleFunctionOutput;

Here, inputPointer is the input pointer, and sampleFunctionOutput is the output pointer. The * symbol is used to declare a pointer in Objective-C.

Example 1: In this example, we are going to understand how to pass pointers to a function.

ObjectiveC




// Objective-C program to illustrate how 
// to pass pointer to a function
#import <Foundation/Foundation.h>
  
@interface SampleClass : NSObject
  
// Declare a method that takes an int pointer as argument
-(void) sampleFunction:(int *) inputPointer; 
  
@end
  
@implementation SampleClass
-(void) sampleFunction:(int *) inputPointer 
{
    // Modify the value pointed by inputPointer
    *inputPointer = 20; 
}
@end
  
int main() 
{
    int sampleValue = 10;
      
    // Create a pointer to sampleValue
    int *inputPointer = &sampleValue; 
    SampleClass *obj = [[SampleClass alloc] init];
      
    // Pass the pointer to the function
    [obj sampleFunction:inputPointer]; 
    printf("Value after passing to function: %d\n", sampleValue);
    return 0;
}


Output:

Value after passing to function: 20

Example 2: In this example, we are going to understand how to return Pointer from Function.

ObjectiveC




// Objective-C program to illustrate how 
// to pass pointer to a function
#import <Foundation/Foundation.h>
  
@interface SampleClass : NSObject
  
// Declare a method that returns an int pointer
-(int *) sampleFunctionOutput; 
@end
  
@implementation SampleClass
-(int *) sampleFunctionOutput
{
  
    // Define a static variable
    static int sampleValue = 30; 
      
    // Return the address of the static variable
    return &sampleValue; 
}
@end
  
int main() 
{
    SampleClass *obj = [[SampleClass alloc] init];
      
    // Call the function and store the returned pointer
    int *outputPointer = [obj sampleFunctionOutput]; 
    printf("Value from function: %d\n", *outputPointer);
    return 0;
}


Output:

Value from function: 30


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads