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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:06:16+00:00 2026-06-12T11:06:16+00:00

I need to create a completely generic linked list that can contain any type

  • 0

I need to create a completely generic linked list that can contain any type of data specified by an enum…

A node of the list has the structure:

  __________________
  |_____|_____|_____|

The first field is of sizeof(nodeType) bytes containing the type of information stored. The next field has an address that contains address of the information variable. The next field has the address of the next node which can be a simple node or yet another linked list..

Basically, I have a nodeType enum as:

 typedef enum{
    STRING, INT, NIL, LIST
 } nodType;

I have allocated memory to a node pointer like this:

 nodeType* node = malloc(sizeof(nodeType) + 2*sizeof(char*));

The first sizeof(nodeType) bytes contain the type of inofrmation stored. I assigned it the value STRING by the expression:

 *node = STRING;

Now, I want the next sizeof(char*) bytes store the address of a char* pointer. (All pointers are of the same size on a machine?(yes, acc to me)).. So, I assign a value to it like:

 char* str = strdup("Hello");
 (char**)(char*(node) + sizeof(nodeType)) = &str;

But GCC flags an error as the LHS of assignment operator is not an lvalue..
I need to assign value to that address in order to go ahead to build that list.
Is there some elegant way of doing that in c apart from using a struct??

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

    You forgot to dereference:

    *(char***)((char*)node + sizeof(nodeType)) = &str;
    

    The result of a dereference operation is always an lvalue. In general, if you want to treat a memory lo­ca­tion p as if it pointed to a variable of type T, you need to cast it to a T * and dereference:

    *(T*)(p) = my_t_value;
    

    This applies to your situation for T = char ** and p = (char *) node + sizeof(nodeType).

    But this is just terrible design. No sane could should contain ***. And moreover, you’re potentially vio­la­ting alignment constraints by assuming that all your elements follow contiguously in memory. A much sim­pler way would be something like this:

    struct Node
    {
        struct Node * next;
    
        nodType type;
        void * data;
    };
    

    Usage:

    struct Node * p = malloc(sizeof *p);
    p->next = NULL;
    p->type = STRING;
    p->data = str;
    

    Note that I chose to store a string directly as a char *, and not as a pointer to a char *. The uni­fy­ing theme should be that the list node owns p->data and should say free(p->data); upon node re­moval unconditionally.

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

Sidebar

Related Questions

I have a problem that I really need solved in any way (changing data
I'm trying to create a generic Colorbox launcher for several links that need to
I am new to Zkoss and i need to create completely new css theme
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I
I want to create a completely generic treeview like structure. some thing like this:
I need to create a file that is locked against reads, at the point
I have created Generic Repository and I have two entities that i need to
I need to create a porting software that will convert Java code to C#

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.