Passing Pointers to Functions in Objective-C
Last Updated :
28 Apr, 2025
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.
- Constant Pointers: When a pointer is declared as a constant, its value cannot be changed.
- 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.
- Input Pointers: Input pointers are used to pass values into a function.
- 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
Similar Reads
Passing Pointers to Functions In C++ Prerequisites: Pointers in C++Functions in C++ Passing Pointers to functions means declaring the function parameter as a pointer, at the function calling passing the address of the variable and that address will be stored by a parameter that is declared as a pointer. To change the value of any varia
4 min read
Return Pointer From Functions in Objective-C A pointer is like a variable or constant that stores the address of another variable. It must be declared before the address value of a variable is to be stored in it. The main advantage of using a pointer is that it frees up the program memory and provides direct access to memory locations. It also
5 min read
Pointer to Pointer in Objective-C Pointers in Objective-C are a powerful and essential concept for any programmer to master. Pointers allow you to manipulate data stored in memory directly and are used to store the address of a variable. Pointer-to-pointer also known as a double pointer, is a type of pointer that holds the address o
4 min read
Functions in Objective-C Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs), Cocoa and
8 min read
Passing Structures as Function Arguments in Objective-C Objective-C is a high-level programming language that is commonly used for developing applications for Apple's Mac OS X and iOS operating systems. It is an object-oriented language that is designed to be easy to use and read, and it provides a powerful set of tools for building complex applications.
6 min read
Pointers in Objective-C In Objective-C, Pointers are the variables that hold the memory address of another variable. You must have declared the pointer variable before its use. The size of the pointer depends on the architecture of the system. The pointer variable can be defined as a char, int, float, double, or any other
5 min read