Q:

Swift program to create a structure within structure

belongs to collection: Swift Structures Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Swift program to create an array of the structure... >>
<< Swift program to create a new structure object wit...