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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:28:22+00:00 2026-05-29T04:28:22+00:00

I am attempting to do my own implementation of common data structures in C

  • 0

I am attempting to do my own implementation of common data structures in C for my own learning benefit. My current effort is a vector, and I want it to be able to hold a single arbitrary type (or at least type size, but isn’t that all that really matters in C?). My struct is as follows:

struct vector
{
    void *item;
    size_t element_size;
    size_t num_elements;
}

However, what I don’t understand is how I can refer to specific elements in the *item array if the type is supposed to be arbitrary. I know the element_size, but that doesn’t help me with index referencing (e.g. item[5]) because void is not a type. I figured it would be easiest to refer to the elements as byte offsets. So if I was holding a vector of structs with size 12, item[5] would be at 12*5=60 bytes from item*. However, I don’t understand how to retrieve that data. I know I want 12 bytes from item+60, but how would I make the compiler understand that? Am I getting into preprocessor territory?

  • 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-29T04:28:22+00:00Added an answer on May 29, 2026 at 4:28 am

    sizeof is measure in chars, so you can do this:

    void *start_of_sixth_element = ((char*)item) + (5 * element_size);
    

    Someone who knows what the type of each element is could then cast start_of_sixth_element to the correct type to use it.

    void* is a bad choice in a way, since you can’t do pointer arithmetic with void* pointers in standard C (there’s a GNU extension to allow it, but for portable code you use char* or unsigned char* at least for the arithmetic).

    Code that knows the correct type before applying the offset 5, could just do this:

    correct_type *sixth_element = ((correct_type *)item) + 5;
    

    void is not a type

    It is a type, it’s just not a “complete type”. “Incomplete type” pretty much means the compiler doesn’t know what a void* really points to.

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

Sidebar

Related Questions

I'm attempting to override and implement my own TabExpansion. In the function I want
I am attempting to write my own data extension for SSRS 2008 r2. I
I am attempting to create my own custom Autocomplete style dropdown control in c#
I am attempting to set up my own project using a third party library
I am attempting to construct my own date picker using code from several sources.
I am attempting to construct my own date picker using code from several sources.
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to make a NSObject called 'Person' that will hold the login details for
I'm attempting to write my own Log4Net Glimpse plugin (so I can interface with
Currently I'm attempting to write my own wxObject, and I would like for the

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.