Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6708863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:48:40+00:00 2026-05-26T07:48:40+00:00

Is it possible to have an array of multiple types by using malloc ?

  • 0

Is it possible to have an array of multiple types by using malloc?

EDIT:

Currently I have:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define int(x) *((int *) x)


int main() {
        void *a[10];

        a[0] = malloc(sizeof(int));
        int(a[0]) = 4;

        char *b = "yola.";

        a[1] = malloc(strlen(b)*sizeof(char));
        a[1] = b;

        printf("%d\n", int(a[0]));
        printf("%s\n", a[1]);
}

But it’s messy. Other ways?

EDIT: Cleaned it up a bit.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:48:41+00:00Added an answer on May 26, 2026 at 7:48 am

    You can’t have an array of different types, exactly. But you can achieve a similar effect (for some purposes at least) in a number of different ways.

    If you just want a few values of different types packaged together, but the number and types of values don’t change, you just need a struct and can access them by name:

    struct s_item {
      int     number;
      char    str[100];
    } item;
    item.number = 5;
    strcpy(item.str,"String less than 100 chars");
    

    If you know what types you might use, you can create a union, or a struct containing a union so you can tag it with the type. You can then create an array of those. The type member lets you check to see what you stored in each array element later.

    enum ElementType { et_str, et_int, et_dbl };
    struct Element {
      ElementType type;
      union {
        char      *str;
        int       i;
        double    d;
      }
    };
    
    struct Element *arr = malloc(sizeof(struct Element) * 3);
    arr[0].type = et_str;
    arr[0].str = strdup("String value"); /* remember to free arr[0].str */
    arr[1].type = et_int;
    arr[1].i = 5;
    arr[2].type = et_dbl;
    arr[2].d = 27.3;
    
    /* access the values.. */
    for (int i = 0; i < 3; i++) {
      switch(arr[i].type) {
        case et_str: printf("String: %s\n",arr[i].str); break;
        case et_int: printf("Integer: %d\n",arr[i].i); break;
        case et_dbl: printf("Double: %f\n",arr[i].d); break;
      }
    }
    
    /* The strings are dynamically allocated, so free the strings */
    for (int i = 0; i < 3; i++)
      if (arr[0].type == et_str) free(arr[0].str);
    /* free the malloc'ed array */
    free(arr);
    /* etc., etc. */
    

    This approach may waste space because:

    • Each element has an extra value to keep track of the type of data it holds
    • The struct may have extra padding between its members
    • The types in the union may be different sizes, in which case the union will be as large as the largest type

    If you have another way of knowing what type you’ve stored in each element, you can use just the bare union without the struct wrapping it. This is a little more compact, but each element will still be at least as large as the largest type in the union.


    You can also create an array of void * values. If you do this, you’ll have to allocate the items somehow and assign their addresses to the array elements. Then you’ll need to cast them to the appropriate pointer type to access the items. C doesn’t provide any runtime type information, so there’s no way to find out what type of data each element points at from the pointer itself — you must keep track of that on your own. This approach is a lot more compact than the others when the types you’re storing are large and their sizes vary a lot, since each is allocated separately from the array and can be given only the space needed for that type. For simple types, you don’t really gain anything over using a union.

    void **arr = malloc(3 * sizeof(void *));
    arr[0] = strdup("Some string"); /* is a pointer already */
    arr[1] = malloc(sizeof(int));
    *((int *)(arr[1])) = 5;
    arr[2] = malloc(sizeof(double));
    *((double *)(arr[2])) = 27.3;
    
    /* access the values.. */
    printf( "String: %s\n", (char *)(arr[0]) );
    printf( "Integer: %d\n", *((int *)(arr[1])) );
    printf( "Double: %f\n", *((double *)(arr[2])) );
    
    /* ALL values were dynamically allocated, so we free every one */
    for (int i = 0; i < 3; i++)
      free(arr[i]);
    /* free the malloc'ed array */
    free(arr);
    

    If you need to keep track of the type in the array, you can also use a struct to store the type along with the pointer, similar to the earlier example with the union. This, again, is only really useful when the types being stored are large and vary a lot in size.

    enum ElementType { et_str, et_int, et_dbl };
    struct Element {
      ElementType type;
      void        *data;
    };
    
    struct Element *arr = malloc(sizeof(struct Element) * 3);
    arr[0].type = et_str;
    arr[0].data = strdup("String value");
    arr[1].type = et_int;
    arr[1].data = malloc(sizeof(int));
    *((int *)(arr[1].data)) = 5;
    arr[2].type = et_dbl;
    arr[2].data = malloc(sizeof(double));
    *((double *)(arr[2].data)) = 27.3;
    
    /* access the values.. */
    for (int i = 0; i < 3; i++) {
      switch(arr[i].type) {
        case et_str: printf( "String: %s\n", (char *)(arr[0].data) ); break;
        case et_int: printf( "Integer: %d\n", *((int *)(arr[1].data)) ); break;
        case et_dbl: printf( "Double: %f\n", *((double *)(arr[2].data)) ); break;
      }
    }
    
    /* again, ALL data was dynamically allocated, so free each item's data */
    for (int i = 0; i < 3; i++)
      free(arr[i].data);
    /* then free the malloc'ed array */
    free(arr);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a variant array which holds nothing but simple types, and possible
Possible Duplicate: php multi-dimensional array remove duplicate I have an array like this: $a
Is it possible to start an array at an index not zero...I.E. you have
I have an array of strings: [username, String, password, String] And I want to
I have a buffer, essentially an char array, filled with multiple different structs. The
Possible Duplicate: Sort multidimensional array by multiple keys I want know how I can
Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a
I have a number of very long arrays. No run-time sort is possible. It
Is it possible have two projects with the same name in flex builder? Here
is possible to have a separator between elements of a GridView? Thanks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.