Open In App

Pointers to Structures in Objective C

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

A pointer is a variable whose value is the address of another variable, e.g., stores the address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store variable addresses. It simplifies the programs and reduces their length. It is useful for allocating and deallocating memory during program execution. It improves the execution speed of a program. A pointer is created using *, it is known as dereference operator. It works on a pointer and gives the value stored in that pointer. & also known as a unary operator to return the memory address of its operand.

Syntax:

DataType *VariableName;

Example:

int* geekI, adres;

geekI = 5;

adres = &c;

The actual numbers stored in the pointer vary with different calls to the same program on the same system. On different computers of the same type as the original, they will differ, on different types and will also differ.

Pointers to Structures

A pointer can also use to point to the address of the memory stored in a structure. In objective C, we can easily point to the structure using pointers with the help of the following steps:

Step 1: Defining a Pointer to the structure:

struct Students *struct_pointer;

Step 2: Storing the address of a structure in a variable using & operator: 

struct_pointer = &Students;

Step 3: Accessing the member of the structure we use the -> operator:

struct_pointer->Name;

Example 1:

ObjectiveC




#import <Foundation/Foundation.h>
  
// Defining a pointer to a structure
struct Students
{
    NSString *Name;
    NSString *Branch;
    int id;
};
  
// Creating a interface 
@interface GFG:NSObject
  
// Declaring a function
- (void) studentData:( struct Students *) student;
@end
  
// Implementing the interface
@implementation GFG 
  
// Implementing the function
- (void) studentData:(struct Students *) student{
  
    // Accessing member of the structure 
    NSLog(@"Student name: %@\n", student->Name);
    NSLog(@"Student branch: %@\n", student->Branch);
    NSLog(@"Student id : %d\n", student->id);
}
@end
  
// Driver code
int main( )
{
  
    // Declaring student of type Student
    struct Students student; 
      
    // Student information     
    student.Name = @"Ayush Agarwal";
    student.Branch = @"CSE";
    student.id = 190;
      
    GFG *exclass = [[GFG alloc]init];
      
    // Displaying the data of student by passing
    // the address of student
    [exclass studentData:&student];
      
    return 0;
}


Output:

Student name: Ayush Agarwal
Student branch: CSE
Student id: 190

Example 2:

ObjectiveC




#import <Foundation/Foundation.h>
  
// Defining a pointer to a structure
struct Car
{
    NSString *Name;
    NSString *Color;
    int modelNumber;
};
  
// Creating a interface 
@interface GFG:NSObject
  
// Declaring a function
- (void) carData:(struct Car *) car;
@end
  
// Implementing the interface
@implementation GFG 
  
// Implementing the function
- (void) carData:(struct Car *) car{
  
    // Accessing member of the structure 
    NSLog(@"Car name: %@\n", car->Name);
    NSLog(@"Car color: %@\n", car->Color);
    NSLog(@"Car model : %d\n", car->modelNumber);
}
@end
  
// Driver code
int main()
{
  
    // Declaring car of type Car
    struct Car car; 
      
    // Car details  
    car.Name = @"Buggati";
    car.Color = @"Silver";
    car.modelNumber = 1234656;
      
    GFG *C = [[GFG alloc]init];
      
    // Displaying the data of car by passing
    // the address of car
    [C carData:&car];
      
    return 0;
}


Output:

Car name: Buggati
Car color: Silver
Car model : 1234656


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads