Structures in Objective-C

Objective-C is an object-oriented programming language that extends the C language with object-oriented capabilities. It follows a bottom-up approach and supports both procedural and object-oriented paradigms. One of the data structures supported by Objective-C is the structure. This article covers how to define and use structures in Objective-C.

Structures

structure is a user-defined data type in Objective-C that allows grouping different data types together. A structure enables you to combine multiple types of data into a single unit. For instance, if we want to store a student’s information, such as their name (string), trade (string), registration number (integer), and age (integer), we can use a structure.

Defining a Structure

A structure in Objective-C is created using the struct keyword, followed by the structure name. Within the structure, we can define multiple data members.

Syntax:

struct structure_name {
    data_type1 member1;
    data_type2 member2;
};

It’s possible to declare one or more variables of the structure type at the end of its definition (this step is optional).

Example:

We define a structure called student that holds two strings (for name and trade) and two integers (for registration number and age):

// Creating a structure to store student data
struct student {
  NSString *name;
  NSString *trade;
  int regNo;
  int age;
};
Initializing Structure Members

Unlike some other languages, structure members cannot be initialized during their declaration. Initialization occurs after creating a structure variable, using dot notation (.).

Example:

// Declaring and initializing structure members
struct student st1;

st1.name = @"Student 1";
st1.trade = @"Computer Science";
st1.regNo = 123;
st1.age = 21;
Accessing Structure Members

To access a structure’s data members, use dot notation (.). The structure variable’s name is followed by the dot and the member name.

Syntax:

struct structure_name variable_name;
variable_name.member1;
variable_name.member2;

Example:

// Objective-C program using structure to store student data
#import <Foundation/Foundation.h>

// Defining structure
struct student {
  NSString *name;
  NSString *trade;
  int regNo;
  int age;
};

int main() {
  struct student st1, st2;

  // Initializing st1
  st1.name = @"Student1";
  st1.trade = @"Computer Science";
  st1.regNo = 123;
  st1.age = 21;

  // Initializing st2
  st2.name = @"Student2";
  st2.trade = @"Electronics";
  st2.regNo = 987;
  st2.age = 20;

  // Printing st1 data
  NSLog(@"Student 1 Name: %@\n", st1.name);
  NSLog(@"Student 1 Trade: %@\n", st1.trade);
  NSLog(@"Student 1 RegNo: %d\n", st1.regNo);
  NSLog(@"Student 1 Age: %d\n", st1.age);

  // Printing st2 data
  NSLog(@"Student 2 Name: %@\n", st2.name);
  NSLog(@"Student 2 Trade: %@\n", st2.trade);
  NSLog(@"Student 2 RegNo: %d\n", st2.regNo);
  NSLog(@"Student 2 Age: %d\n", st2.age);

  return 0;
}

Output:

Student 1 Name: Student1
Student 1 Trade: Computer Science
Student 1 RegNo: 123
Student 1 Age: 21

Student 2 Name: Student2
Student 2 Trade: Electronics
Student 2 RegNo: 987
Student 2 Age: 20

Pointers to a Structure

Like primitive data types, you can have pointers to structures. The pointer is defined using the * symbol, similar to other types of pointers.

struct student *student_ptr;

You can store the address of a structure variable in the pointer by using the address-of operator (&):

student_ptr = &st1;

To access members of a structure via a pointer, use the arrow operator (->):

student_ptr->name

Example: Using Pointers to Access Structure Members

// Objective-C program using pointers to access structure members
#import <Foundation/Foundation.h>

// Defining structure
struct student {
  NSString *name;
  NSString *trade;
  int regNo;
  int age;
};

int main() {
  struct student st1;
  struct student *student_ptr;

  student_ptr = &st1;

  // Initializing st1
  st1.name = @"Student1";
  st1.trade = @"Computer Science";
  st1.regNo = 123;
  st1.age = 21;

  // Printing st1 info using direct access
  NSLog(@"Printing st1 info directly\n");
  NSLog(@"Name: %@\n", st1.name);
  NSLog(@"Trade: %@\n", st1.trade);
  NSLog(@"RegNo: %d\n", st1.regNo);
  NSLog(@"Age: %d\n", st1.age);

  // Printing st1 info using pointer
  NSLog(@"\nPrinting st1 info using pointer\n");
  NSLog(@"Name: %@\n", student_ptr->name);
  NSLog(@"Trade: %@\n", student_ptr->trade);
  NSLog(@"RegNo: %d\n", student_ptr->regNo);
  NSLog(@"Age: %d\n", student_ptr->age);

  return 0;
}

Output:

Printing st1 info directly
Name: Student1
Trade: Computer Science
RegNo: 123
Age: 21

Printing st1 info using pointer
Name: Student1
Trade: Computer Science
RegNo: 123
Age: 21

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *