CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C Programming – How to store different data types in an array?

We learned that arrays are the sequence of elements that allows for storing the same data type elements. But, if we want to store different data type elements there are different ways to do this; and we are going to cover one of them in this article.

An array of structures in C

We know that structures can contain different types of elements. If we create an array of structures, it meets our requirement to create an array with different types of elements. But, we need to create our structure cautiously; to allow us to pick the right data and also need to optimize the size of the structure; because the size of the structure is the sum of the size of all its elements.

If we create a structure like the one below, it occupies more memory. And we do not need to store all structure elements in each individual array element. For example, if we create a large array to store the below structure, it occupies more memory.

struct data
{
    char ch;
    int i;
    long int li;
    float f;
    double d;
};

So, we need to tweak our structure in such a way that it occupies less memory. How to do this? It is simple; use union inside the structure. Union creates the memory which can be shared with all elements of it, the overall memory allocation will be the same as the memory required to store the largest element in the union.

And another member we need to add to the structure is to determine the type of the element to use; depending on the type of the element we can use the right element from the union.

Our modified structure looks like the below;

struct <name>
{
     <data type> type; // This is to identify which member to use
    
    union
    {
        <data type> member_1;
        <data type> member_1;
        ...
        ...
        <data type> member_n;
    };
};

Working Example

Now, this is the time to develop a code to create an array that allows storing different types of elements. Here is the complete working example;

#include <stdio.h>

enum datatype { char_type = 1, int_type, longlong_type, float_type, double_type };

struct data
{
	enum datatype type;

	union
	{
		char symbol;
		int value;
		float interest;
		long long int big_number;
		double operational_cost;
	};
};

void main()
{
	struct data arr[] = { 
		{char_type, .symbol='A'},
		{int_type, .value=57},
		{longlong_type, .big_number= 151820000},
		{float_type, .interest=8.5},
		{double_type, .operational_cost=3392917888.78}
	};
	
	for (int i = 0; i < sizeof(arr) / sizeof(struct data); i++)
	{
		switch (arr[i].type)
		{
		case char_type:
			printf("%c", arr[i].symbol);
			break;

		case int_type:
			printf("%d", arr[i].value);
			break;

		case longlong_type:
			printf("%lld", arr[i].big_number);
			break;

		case float_type:
			printf("%f", arr[i].interest);
			break;

		case double_type:
			printf("%f", arr[i].operational_cost);
			break;		
		}

		printf("\n");
	}
}

//Malin

C Programming – How to store different data types in an array?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top