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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:48:27+00:00 2026-05-18T09:48:27+00:00

Hey, I’m a beginner in C and tried to implement my own linked list

  • 0

Hey,
I’m a beginner in C and tried to implement my own linked list implementation, that basically looks like this:

struct Element
{
    void *value;
    struct Element *next;
};

typedef struct
{
    struct Element *first;
    struct Element *last;
    unsigned int size;
} LinkedList;

void LinkedList_init(LinkedList *this)
{
    this->size = 0;
    this->first = NULL;
    this->last = NULL;
}

void LinkedList_add(LinkedList *this, void *value)
{
    struct Element *node = malloc(sizeof(struct Element));
    node->value = value;
    node->next = NULL;

    if (this->size == 0)
        this->first = this->last = node;
    else
    {
        this->last->next = node;
        this->last = node;
    }

    this->size++;
}

So in short, I want a linked list that can hold arbitrary types – I heard, this is possible in C by using void pointers.
The problem now arises, when I want to use that implementation, for example with a structure as value:

typedef struct
{
    int baz;
} Foo;

int main(void)
{
    LinkedList list;
    Foo bar;
    bar.baz = 10;

    LinkedList_init(&list);
    LinkedList_add(&list, (void *) &bar);

    /* try to get the element, that was just added ... */
    Foo *firstElement = (Foo *)list.first;
    /* ... and print its baz value */
    printf("%d\n", firstElement->baz);

    return 0;
}

The last printf call just prints values like -1077927056, which look like a memory address. So it’s probably a problem with pointers. After searching the web the last few days for a similar issue on the web (I had no luck with that), I tried to throw my own logic away and tested various random *& combinations. Turns out, that was a dead end, too. 🙁

It’s probably something simple for a more experienced C programmer, but I just can’t find the answer. Please help 😀

  • 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-18T09:48:28+00:00Added an answer on May 18, 2026 at 9:48 am

    list.fist is a struct Element.

    Try:

    Foo *firstElement = (Foo *)(list.first->value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, today I tried to create a photoshop automation that saves me an area
Hey guys, I just started learning C++ and I have this code that I
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey, Sorry if this is a dumb question. I'm an amateur programmer and new
Hey all, I am not quite getting why this doesn't seem to be working.
Hey guys, I have a little button functionality that is not erring, but also
Hey - is there a way to use the System.currentTimeMillis(); to have convery that
hey i got a contnet type that i made and i want to twweak
Hey I'm trying to creare a link that passes the link's text directly to
Hey, I Have three IQueryable lists which i concat together into one list to

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.