Pointers to Structures in Objective C
Last Updated :
17 Mar, 2023
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
Similar Reads
Structures in Objective-C Objective-C is an object-oriented programming language that adds small talk-style messaging to the C programming language. Follows a bottom-up programming approach. It incorporates concepts from both procedural and object-oriented programming languages. Objective-C support structures. So in this art
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
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
Pointer to Arrays in Objective-C In Objective-C, a pointer to an array is a way to store multiple values of the same data type in contiguous memory locations. These arrays can be manipulated and accessed using pointers, which are variables that store the memory address of another variable. In Objective-C, pointers to arrays are use
4 min read
Array of Pointers in Objective-C A pointer is a variable that stores the address of another variable. We use pointers because, with the help of pointers the memory is accessed efficiently, it saves memory space, and execution time is faster as compared to the use of normal variables using stack memory because pointers store their m
5 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