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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:39:41+00:00 2026-06-01T19:39:41+00:00

I’m trying to make a generic stack in plain C and I have some

  • 0

I’m trying to make a generic stack in plain C and I have some problems with pointers and no idea where is the problem.

Here’s the structure and functions, where I have problems:

typedef struct{
    void *elems; 
    int elemSize; 
    int logLength; 
    int allocLength;
} genStack;

void GenStackPush(genStack *s, const void *elemAddr); 
void GenStackPop(genStack *s, void *elemAddr);

That’s the implementation:

void GenStackPush(genStack *s, const void *elemAddr)
{
    s->elems[s->logLength] = elemAddr;
    s->logLength++;
}

void GenStackPop(genStack *s, void *elemAddr)
{
      s->logLength--;
      elemAddr = s->elems[s->logLength];
}

The usage should look like this:

int val; 
genStack IntegerStack;
for (val = 0; val < 6; val++)
    GenStackPush(&IntegerStack, &val);

GenStackPop(&IntegerStack, &val); 
printf("Popped: %d\n",val);

And here are the problems I get:

genstacklib.c: In function ‘GenStackPush’:
genstacklib.c:60:10: warning: dereferencing ‘void *’ pointer [enabled by default]
genstacklib.c:60:2: error: invalid use of void expression
genstacklib.c: In function ‘GenStackPop’:
genstacklib.c:72:23: warning: dereferencing ‘void *’ pointer [enabled by default]
genstacklib.c:72:13: error: void value not ignored as it ought to be

I have tried already several ways to fix the code, but none of them worked.
Thanks.

==========================================================================

So, guys, thanks for help! Now it compiles, but I have changed an API, which was given by our Professor. There was also the problem with ‘const’ qualifier, so I deleted them. Not my code looks like this:

genstacklib.h:

#ifndef GENSTACKLIB_H
#define GENSTACKLIB_H
#define GenStackInitialAlocationSize 4

typedef struct{
    void** elems;
    int elemSize;
    int logLength;
    int allocLength;
}genStack;

void GenStackNew(genStack *s,int elemSize);
void GenStackDispose(genStack *s);
int GenStackEmpty(const genStack *s);
void GenStackPush(genStack *s, void *elemAddr);
void GenStackPop(genStack *s, void *elemAddr);

#endif

genstacklib.c:

#include <stdlib.h>
#include <stdio.h>
#include "genstacklib.h"

void GenStackNew(genStack *s,int elemSize)
{
    void** newElems;

    /* Allocate a new array to hold the contents. */
    newElems = (void**) malloc(elemSize * GenStackInitialAlocationSize);

    if (newElems == NULL)
    {
        fprintf(stderr, "Error with allocating the stack.\n");
        exit(1); /* Exit, returning error code. */
    }
    s->elems = newElems;
    s->allocLength = GenStackInitialAlocationSize;
    s->logLength = 0; /*is empty*/

}

void GenStackDispose(genStack *s)
{
    s->allocLength = 0;
    free(s->elems);
}

int GenStackEmpty(const genStack *s)
{
    return s->logLength == 0;
}

void GenStackPush(genStack *s, void *elemAddr)
{
    s->elems[s->logLength] = elemAddr;
    s->logLength++;
}

void GenStackPop(genStack *s, void *elemAddr)
{
      s->logLength--;
      elemAddr = s->elems[s->logLength];
}

If you have any ideas to improve it or something to say about it, I would hear with pleasure. 😀

  • 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-01T19:39:43+00:00Added an answer on June 1, 2026 at 7:39 pm

    you are trying to dereference void pointer without typecasting to some other type which causing the problem.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.