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 8535771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:30:40+00:00 2026-06-11T10:30:40+00:00

How to implement a stack using array that supports items of different type. e.g.

  • 0

How to implement a stack using array that supports items of different type.
e.g. It should operate on characters, integers, floats and doubles.

I have implemented it using void pointers. Below is the C implementation:

void push( void** stack, int* top, void* data, size_t size )
{
    unsigned i;
    ++*top;

    stack[*top] = malloc( size );

    for( i = 0; i < size; ++i )
            ( (char*)stack[*top] )[i] = ( (char*)data )[i];

}

int main()
{
    void* stack[10];
    int top = -1, data = 10;
    char ch = 'a';

    push( stack, &top, (void*)&data, sizeof( int ) );

    push( stack, &top, (void*)&ch, sizeof( char ) );        

    printf( "%d ", *(int*)stack[0] );
    printf( "%c ", *(char*)stack[1] );

    return 0;
}

The code works fine for me.

The problem with the above implementation is that the type of data must be known before-hand.

Does there exists a way to implement it without knowing prior information about type of data to be operated [ I know it is not possible in C, Can we do it in C++, if Yes, How? ]?

  • 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-06-11T10:30:41+00:00Added an answer on June 11, 2026 at 10:30 am

    If I understand your requirement, you can use boost::any to achieve this in C++.

    #include <boost/any.hpp>
    #include <vector>
    
    class AnyStack {
        std::vector<boost::any> vec;
    public:
        template <class T> void push (const T &e) {
            boost::any v = e;
            vec.push_back(v);
        }
        class Proxy {
            friend class AnyStack;
            std::vector<boost::any> &vec;
            Proxy(std::vector<boost::any> &v) : vec(v) {}
        public:
            template <typename T> operator T () {
                boost::any v = vec.back();
                vec.pop_back();
                return boost::any_cast<T>(v);
            }
        };
        Proxy pop () { return Proxy(vec); }
        boost::any top () { return vec.back(); }
    };
    

    As others have suggested, you can use RTTI to determine the type of the item in the stack. The example below demonstrates an output routine that does this.

    #include <iostream>
    #include <string>
    
    std::ostream & operator << (std::ostream &os, const boost::any &a) {
        if (a.type() == typeid(char)) {
            return os << boost::any_cast<char>(a);
        }
        if (a.type() == typeid(int)) {
            return os << boost::any_cast<int>(a);
        }
        if (a.type() == typeid(float)) {
            return os << boost::any_cast<float>(a);
        }
        if (a.type() == typeid(double)) {
            return os << boost::any_cast<double>(a);
        }
        if (a.type() == typeid(std::string)) {
            return os << boost::any_cast<std::string>(a);
        }
    }
    
    int main () {
        AnyStack a;
        a.push(3);
        std::cout << a.top() << std::endl;
        a.push(std::string("hello"));
        std::cout << a.top() << std::endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we implement a stack using a dynamically allocated array. If the array fills,
I am trying to implement a Stack in java (using the list interface: Interface
How would you implement a generic stack without using nsmutablearrays?
A language has not array as a data type but it has stack as
I am using JavaScript to store values in a stack. The problem is that
I'm trying to implement a tab menu just like the one in Stack Overflow.
I've recently been implementing a recursive directory search implementation and I'm using a Stack
So, I know some Python but I thought that I should try to add
I have two queues, one is implemented using an array for storage and the
My idea is to implement something like this: https://i.stack.imgur.com/8BmW5.png screenshot is from here: http://www.thedrum.co.uk/opinion/2012/03/08/five-ways-new-facebook-timeline-will-impact-brands

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.