Structures in Objective-C
Last Updated :
26 Apr, 2025
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
Similar Reads
Pointers to Structures in Objective C
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
3 min read
Strings in Objective-C
Strings are a fundamental concept in programming, and they are used to represent text in many different applications. In Objective-C, a string is an object that represents a sequence of characters. A string can be created using several built-in classes and methods, which allow you to manipulate the
4 min read
Operators in Objective-C
The Objective-C language is developed on top of C Programming. The operator in Objective-C is the same as the C language operators. It is primarily used in developing iOS and MacOS operating systems and software applications for iOS. Operators are used to forming a mathematical expression using vari
11 min read
Properties in Objective-C
The object-oriented programming language Objective-C is largely used to create applications for Apple's macOS and iOS platforms. It incorporates all of the characteristics of C while also providing more features for object-oriented programming, making it a superset of the C programming language. The
5 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
Variables in Objective-C
A variable is a place for data storage that our programs can access or manipulate. In Objective-C, variables have a defined type that specifies their shape and layout. They also cover the gamut of values and operations that we are capable of performing. Before moving further, we understand the guide
4 min read
Preprocessors in Objective-C
Preprocessors help the software experts in tasks like Reusability and Conditional compilation. This article focuses on discussing Preprocessors in Objective-C. What is Objective-C?The Objective-C language is a general-purpose, dynamic, and high-level programming language designed to enable object-or
6 min read
Data Types in Objective-C
In Objective-C, each variable has an associated data type and each data type has different amounts of memory required in a system. A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. Or in other words, a data type represents what
8 min read
Typedef in Objective C
A typedef allows us to define a data type with a new name, for example, typedef unsigned char CAT. Now from now, onwards CAT can be used as an abbreviation for unsigned char, for example, CAT num1, num2;. Or in other words, we can say that typedef is a keyword that is used to assign the new name to
3 min read
Posing in Objective-C
In Objective-C, posing is a technique that allows a class to wholly replace another class within a program. The replacing class is said to "pose as" the target class. All messages sent to the target class are instead received by the posing class. This article focuses on discussing posing. What is Po
5 min read