Open In App

Structures in Objective-C

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

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 article, we will learn about structures and their usage.

Structures

A structure is a user-defined data type in Objective-C programming that allows users to combine different data elements of different types. A structure creates a data type that can be used to combine data items that may be of different types into one data type. For example, if we want to store a student’s data that includes his/her name in the string, his/her trade in the string, his/her registration number in int, and his/her age in int, we will use structure.

Creating a structure

The structure is created using the ‘struct’ keyword followed by the name of the structure. In the structure, we can store as many data members as many we want.

Syntax

struct structure_name{

data_type1 data_member1;

data_type2 data_member2;

};

You can also specify one or more structure variables at the end of the structure’s definition(before the semicolon). It is optional. 

Example:

Here we are creating a structure named student which will store two strings and two int values for name, trade, registration number, and age respectively.

ObjectiveC




// Creating a structure which will store student's data
struct student {
  NSString *name;
  NSString *trade;
  int regNo;
  int age;
};


Initialize values structure’s data members

Structure members cannot be initialized with the declaration. For example, the following program fails in the compilation.

ObjectiveC




// The followwing code will result in compile error
struct student {
   NSString *name = "";
   NSString *trade = "";
   int regNo = 1;
   int age = 20;
};


The reason for the above error is that when a datatype is declared, no memory is allocated for it. Memory is allocated only when variables are created. We initialize data members by creating a variable of struct data type and then using dot notation(.).

ObjectiveC




// Giving values data members
struct student st1;
  
st1.name = @"Student 1";
st1.trade = @"Comp Science";
st1.regNo = 123;
st1.age = 21;


Accessing Data Members

To access struct data members, we use dot notation(.). Here the structure variable name is followed by dot notation and the structure member that you want to access. 

Syntax

struct struct_name variable_name;

data_type1 = variable_name.data_member1;

data_type2 = variable_name.data_member2;

ObjectiveC




// Objective-C program of structure
#import <Foundation/Foundation.h>
  
// Creating structure
struct student {
  NSString *name;
  NSString *trade;
  int regNo;
  int age;
};
  
int main() {
  
  /* Declare st1 of type student */
  struct student st1;  
    
  /* Declare st2 of type student */
  struct student st2;       
  
  /* st1 */
  st1.name = @"Student1";
  st1.trade = @"comp Science"
  st1.regNo = 123;
  st1.age = 21;
  
  /* st2 */
  st2.name = @"Student2";
  st2.trade = @"Electronics";
  st2.regNo = 987;
  st2.age = 20;
  
  /* printing st1 info */
  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 info */
  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 : comp 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

Pointer to a Structure

Like primitive data types, we can also have a pointer to a structure. So to define a pointer to a structure we use (*) just like we define a pointer to any other variable:

 struct student *struct_ptr_st2;

To store the address of the structure variable in the above pointer variable we use & operator:

 struct_ptr_st2 = &st1;

If we have a pointer to the structure, members are accessed using the arrow ( -> ) operator. 

struct_ptr_st2->name

A pointer to a structure allows the data members of the structure to be accessed by the pointer structure also. The same is shown in the example given below.

ObjectiveC




// Objective-C program of pointer to structure
#import <Foundation/Foundation.h>
  
// Creating structure
struct student {
   NSString *name;
   NSString *trade;
   int regNo;
   int age;
};
  
int main()
{
   /* Declare st1 of type student */
   struct student st1;      
     
   /*Declaring pointer to structure */
   struct student *struct_ptr_st2; 
   struct_ptr_st2 = &st1;
  
   /* st1 */
   st1.name = @"Student1";
   st1.trade = @"comp Science";
   st1.regNo = 123;
   st1.age = 21;
  
   /* printing st1 info using st1*/
   NSLog(@"Printing info of st1 using st1\n");
   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 st1 info using struct_ptr_st2*/
   NSLog(@"\nPrinting info of st1 using struct_ptr_st2\n");
   NSLog(@"Student 1 name : %@\n", struct_ptr_st2->name);
   NSLog(@"Student 1 trade : %@\n", struct_ptr_st2->trade);
   NSLog(@"Student 1 regNo : %d\n", struct_ptr_st2->regNo);
   NSLog(@"Student 1 age : %d\n", struct_ptr_st2->age);
  
   return 0;
}


Output

Printing info of st1 using st1
Student 1 name : Student1
Student 1 trade : comp Science
Student 1 regNo : 123
Student 1 age : 21
Printing info of st1 using struct_ptr_st2
Student 1 name : Student1
Student 1 trade : comp Science
Student 1 regNo : 123
Student 1 age : 21


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads