Open In App

Return Pointer From Functions in Objective-C

Last Updated : 21 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 allows the manipulation of data which makes the program execution times faster. It provides effective memory access, and dynamic memory allocation and is widely used in file handling. With the help of a pointer, we can symbolically represent the memory locations and it must be declared before using it to store any address.

Return Pointer from Functions 

Like other programming languages, Objective-C also allows returning a pointer from user-defined functions. To do the same, we need to make a function that returns a pointer of any variable. The data type of the pointer and the return type of the function which we are using to return the pointer must be the same. The function that is used to return a pointer cannot be void type even if the pointer to be returned is null.

Syntax:

data_type * demoFunction() {

// function snippet code

}

Example 1:

ObjectiveC




// Objective-C program to return pointer from functions
#import <Foundation/Foundation.h>
  
/* Declaring a global variable */
int c = 5;
  
/* Function to return a pointer value */
int* returnPointer() 
{
    int *ptr;
    ptr = &c;
      
    // Returning pointer from function
    return ptr;
}
   
/*
Main function declared to call the 
above user-defined function 
*/
  
int main () 
{
     
    // Declaring a pointer of type int
    int *p;
      
    // Returning the pointer value in the
    // declared pointer variable
    p = returnPointer();
      
    // Printing the pointer value
    NSLog(@"Value of the *p variable is %d.\n", *p);
    
    return 0;
}


Output:

Value of the *p variable is 5.

Explanation: The above code assigns a value to the pointer p with the help of a user-defined function. We initialize a pointer p without assigning any value to it. A function is created that returns a pointer and the value that is to be returned is assigned to our pointer p. In the user-defined function, we initialize a local pointer and assign it the address value of the global variable we have declared on top. The main aim of this technique is to get the required value inside our local pointer variable and then return that value in the pointer p of our main function.

Example 2:

ObjectiveC




// Objective-C program to return pointer from functions
#import <Foundation/Foundation.h>
  
/* function to return a pointer value */
int* returnPointer(int a[]) 
{
    a = a + 2;
      
    // Returning pointer from function
    return a;
}
   
/* 
main function declared to call the 
above user-defined function 
*/
  
int main ()
{
     
    // Declaring a pointer of type int
    int *p;
    int a[] = {1, 2, 3, 4, 5};
      
    // Returning the pointer value in the
    // declared pointer variable
    p = returnPointer(a);
      
    // Printing the pointer value
    NSLog(@"Value of the *p variable is %d.\n", *p);
      
    return 0;
}


Output:

Value of the *p variable is 3.

Explanation: The above code accesses the value of an array with the help of a user-defined function. We initialize a pointer p and an array of finite sizes. The value of pointer p is assigned the return value of the user-defined function, inside which we pass the array as a parameter. In the user-defined function, the memory location of the first array value is passed by default. We directly update that value by incrementing it by 2 and then return that value to our pointer p. When the value of pointer p is printed, it outputs the third value of the array.

Note: In Objective-C, we cannot return the address of a variable declared in the local scope outside the function. To do so, either we have to use global variables (as done in example 1) or we can define the local variable as a static variable. This can be done as shown in the example below.

Example 3:

ObjectiveC




// Objective-C program to return pointer from functions
#import <Foundation/Foundation.h>
  
/* Function to return a pointer value */
int* returnPointer()
{
    // Declaring a static variable
    static int q = 4;
    int *ptr;
    ptr = &q;
      
    // Returning pointer from function
    return ptr;
}
   
/*
main function declared to call the 
above user-defined function 
*/
  
int main ()
{
     
    // Declaring a pointer of type int
    int *p;
      
    // Returning the pointer value in the declared pointer variable
    p = returnPointer();
      
    // Printing the pointer value
    NSLog(@"Value of the *p variable is %d.\n", *p);
      
    return 0;
}


Output:

Value of the *p variable is 4.

Explanation: The above code assigns a value to the pointer p with the help of a user-defined function. In the main function, we initialize a pointer p without assigning any value to it. A function is created that returns a pointer and the value that is to be returned is assigned to our pointer p. In the user-defined function, we create a static variable with some value assigned to it. Then we initialize a local pointer and assign it the address value of the static variable we have declared before. The stored value in the local pointer variable is returned by the function and assigned to the pointer p. This is done to avoid returning the local variables used in the function. That’s why a static variable is declared in the function so as to assign a value to the local pointer.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads