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

  • Home
  • SEARCH
  • 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 9122427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:06:11+00:00 2026-06-17T06:06:11+00:00

I am trying to implement a generic stack in c using void pointer to

  • 0

I am trying to implement a generic stack in c using void pointer to point to the data. the structure looks like this

struct record{
  void* data;
  struct record* previousRecord; 
};

where void pointer data is a pointer to the data a stack position will hold. If I implement a push function like this

struct record* push(struct record* tos, void* data){
  struct record* newtos = (struct record*)malloc(sizeof(struct record));
  newtos->data = data;
  newtos->previousRecord = tos;
  return newtos;
}

and push a couple of integer pointers and string pointers into the stack, is there any way I can print the values referenced by these pointers. My problem is that I have to specify the data type of value to print in the code if I use a printf function but the values stored in the stack can only be determined at run time

  • 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-17T06:06:12+00:00Added an answer on June 17, 2026 at 6:06 am

    With address how would you know the type of data, it can be string or an integer address:

    But you can keep an extra field in your record definition about type of value stored in data part, like below:

    typedef enum { STRING, INT, CHAR} type;
    struct record{
      type t;
      void* data;
      struct record* previousRecord; 
    };
    

    and write a common print function :

    int print(struct record  *r){  
    
      switch(r->t){
         case CHAR:     return printf("%c", *((char*)(r->data)));
         case INT:      return printf("%d", *((int*)r->data));        
         case STRING:   return printf("%s", (char*)r->data);        
         default:       return printf("Error");
      }
    } 
    

    Here is a Project/A book that can be very helpful to write generic code in C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a generic notification system.. I have a data structure like
I am trying to implement generic repository pattern using LINQ to SQL data context.
I'm trying to implement the repository pattern using a generic repository like the one
I am trying to implement a generic table widget (using KendoUI) having the data
I am trying to implement a generic repository pattern. I found this site which
When using ASP.NET MVC plus Entity Framework, and trying to implement a generic repository
I am trying to implement a generic hashlist class using templates and I am
I'm trying to implement a generic linked list. The struct for the node is
I am trying to implement a generic Wrapper-Class for Qt's class system using C#'s
I'm trying to implement a Generic Repository. This is what I've got so far

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.