Use struct to Handle Heterogeneous Data Elements in C++

Use struct to Handle Heterogeneous Data Elements in C++

Have you ever come across an application where you’re required to handle more than one set of heterogeneous data elements? Operations may include initialising, reading from, writing to, and passing data to different parts of the application.

For exmple, suppose you’ve got an application that deals with employee data?things like ID, name, salary, etc. In these cases, you’d use a “binding device” like a struct, instead of separate variables:

struct Employee{  char		szName[50];  long		nDeptCode;  char		chGrade;  double	dSal;};

There are a number of advantages to using this method:

  1. One-shot initialisation (irrespective of data types inside the structure):
        struct Employee obj1;  memset ((void *)struct Employee obj1,            '',            sizeof(struct Employee obj1));

    All the bytes of all the elements get reset.

  2. One-shot copying of objects:
      struct Employee objAllan;  struct Employee objBen;    objAllan.szName = Allan Davidson;  objAllan.nDeptCode = 2;  objAllan.chGrade = 'A';  objAllan.dSal = 10000;  objBen = objAllan;

    objBen gets identical values in its elements as objAllan.

  3. Flexibility in passing parameters.

The application typically has a set of functions which use some or all of the data elements for their processing. You can always pass the necessary variable and write the implementation as follows:

  void func1(char pchGrade)  {    // ...  }  int func2(char *pszName, double pdSal)  {    // ...  }

However, using a structure, the implementation becomes more precise and readable. The entire data set is communicated using a single identifier. This approach can also give a uniform look to the functions:

  • Passing the argument:
      struct Employee obj1;  // ...  func(obj1);
  • Using the argument:
      void func1(struct Employee st)  {    st.chGrade = 'A'    // ...  }  }
  • Passing the argument:
      struct Employee * pobj1;  // ...  pobj1 = (struct Employee *)malloc(sizeof(struct Employee));  func(pobj1);  // ...  free(pobj1);
  • Using the argument:
      void func2(struct Employee * pst)  {    pst->dSal = 10000.00;    // ... ... ...  }
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing