Open In App

Difference between Swift Structures and C Structure

Swift structures are a basic building block in the Swift programming language. They are used to group related data together in a single unit. They are similar to classes, but differ in several key ways, including:

Overall, Swift structures are a useful tool for grouping related data and encapsulating behavior, and are best used for small, simple data structures. Classes are a better option when you need to model complex relationships or share states between multiple instances.



Example:




// Swift program to illustrate structure
  
// Creating structure 
struct Person 
{
    var firstName: String
    var lastName: String
    var age: Int
}
  
// Creating structure instance
var person = Person(firstName: "John", lastName: "Doe", age: 32)
  
print("\(person.firstName) \(person.lastName) is \(person.age) years old.")
  
// Accessing structure properties
person.age = 33
  
print("\(person.firstName) \(person.lastName) is now \(person.age) years old.")

Output:



John Doe is 32 years old.
John Doe is now 33 years old.

In this example, we define a structure called Person with three properties: firstName, lastName, and age. We then create an instance of the structure called the person and initialize its properties with values. We then use the properties to display a message and modify the age property to show that a structure is a value type.

C structures are a basic building block in the C programming language, used to group related data together in a single unit. A structure is a composite data type that groups variables of different data types under a single name. Each variable within the structure is called a member.

Example:




// C program to illustrate structure
#include <stdio.h>
  
// Creating structure 
struct person 
{
    char firstName[20];
    char lastName[20];
    int age;
};
  
int main() 
{
    // Creating structure instance
    struct person person1;
    
    // Accessing structure properties
    strcpy(person1.firstName, "John");
    strcpy(person1.lastName, "Doe");
    person1.age = 32;
  
    printf("%s %s is %d years old.\n"
           person1.firstName, person1.lastName, 
           person1.age);
    
    return 0;
}

Output:

John Doe is 32 years old.

In this example, we define a structure called a person with three members: firstName, lastName, and age. We then create an instance of the structure called person1 and initialize its members with values using the strcpy and assignment operator. Finally, we display the values using the printf function.

C structures are often used to represent complex data, such as records in a database or objects in an object-oriented program. They can also use to group related variables for easier code organization and maintenance.

Difference between Swift Structures and C Structure

Feature

Swift Structures 

C Structures

Inheritance Swift Structures do not support inheritance.  C Structures do not support inheritance.
Methods and Properties  Swift Structures support both methods and properties.  C Structures only have members.
Initialization Swift Structures have automatic memberwise initializers and custom initializers.  C Structures require manual initialization of each member.
Pass by Value  Swift Structures are passed by value.  C Structures can be passed by value or reference.
Type Safety  Swift Structures have strong typing.  C Structures have looser typing.
Memory Management  Swift Structures have automatic reference counting.  C Structures require manual memory management.
Mutability Properties are non-mutable unless declared as var.  Members can be mutable.
Named vs Anonymous  Swift Structures must have a name.  C Structures can be anonymous and embedded within other structures.
Type Aliasing  Swift Structures can be type-aliased to a new name.  C Structures cannot be type-aliased.

Article Tags :