Swift program to create a structure within structure

Here, we are going to learn how to create a structure within structure in Swift programming language?
Submitted by Nidhi, on July 02, 2021

Problem Solution:

Here, we will create a structure within the structure and access members of both structures.

Program/Source Code:

The source code to create structure within the structure is given below. The given program is compiled and executed successfully.

// Swift program to create structure within structure

import Swift

struct Strct1 {
    struct Strct2 {
        var num2: Int=0
    }

    var num1: Int=0
    var str2=Strct2()
}

var str = Strct1()

str.num1        = 10
str.str2.num2   = 20

print(str.num1)
print(str.str2.num2)

Output:

10
20

...Program finished with exit code 0
Press ENTER to exit console.

Explanation:

In the above program, we imported a package Swift to use the print() function using the below statement,

import Swift

Here, we created a structure struct1 and then also created a nested structure struct2 within struct1. Then we set the value of structure members and printed the result on the console screen.

Swift Structures Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.