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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:34:44+00:00 2026-05-28T13:34:44+00:00

I want to create a generic stack. I want to implement it with a

  • 0

I want to create a generic stack. I want to implement it with a linked-list.

I created this structures stack_l :

typedef struct stack
 {
   void * data;

   void (*copy)(void *o);

   struct stack *next;

 } stack_l;

I have some questions:


  • The second field of the structures is a pointer to a function, to copy a new data (passing by argument in a function Push).
    The prototype of the function Push is:

stack_l * Push( stack_l * head, void * d );

  1. Is it correct how I pass the pointer to the function copy?
  2. I have to implement it in the function Push??

  • Is it necessary create a function NewStack (for example) to inizialize the field of the structures, or is better have only a Push function that create the 1st element if the stack is empty, and add new element on top if there is at least one element?

  • A void * need to be allocated with a malloc ?
  • 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-28T13:34:45+00:00Added an answer on May 28, 2026 at 1:34 pm

    First of all, you do not require different identifiers for stack and stack_l. Now, to your questions:

    Your Push function is incompatible with the type of the copy field in stack_l. If you want to include a pointer to (member-)functions in your struct to make it look like C++, you still have to pass your object to these functions.

    Yes ,you have to implement your functionality somewhere, and that cannot be within the definition of struct stack. This means that you do need some sort of constructor function (you may call it newStack if you like). Your constructor must at least initialise your function pointers (if you want to keep them). If you opt not to keep these function pointers you can put the initialisation code well in your push routine, as you suggested:

    stack_l* stackPush(stack_l* head, void* content) {
      // head might be NULL, meaning the stack is "empty" or it might be an actual pointer
      // we don't care
      stack_l* const front = malloc(sizeof(stack_l));
      *front = (stack_l){.data = content, .next = head};
      return front;
    }
    

    Note that you have to explicitly state what the empty stack is. So you may want to implement a constructor for clarity:

    stack_l* stackCreateEmpty() {
      retun NULL;
    }
    

    And maybe even a destructor:

    void stackDestroyEmpty(stack_l* s) {
      assert(s == NULL && "Tried to destroy non-empty stack.");
    }
    

    To your last question: As you see above, you have to use malloc to get space to hold your stack_l object, other than that, you do not need to allocate extra space for your void* member, as it is part of stack_l.

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

Sidebar

Related Questions

I want to create a generic list - but I want to specify the
I want to create a Generic DataGrid to use on all my Views/UserControls. This
I want to create a KeyValue class but in generic manner and this is
I want to create a generic list of the Type object. I have ...
I want to create a generic method to serizlize a class to text (for
I want to create a generic IEnumerable implementation, to make it easier to wrap
I'm trying to create a UniqueAttribute using the System.ComponentModel.DataAnnotations.ValidationAttribute I want this to be
I want create a generic (general) method which accepts the return types of LINQ
I want to create a generic class that takes a type parameter and restrict
I want to create a generic to which I can pass a function as

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.