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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:05:21+00:00 2026-05-14T18:05:21+00:00

Suppose I have the following: typedef struct { int itemSize; int count; void *list;

  • 0

Suppose I have the following:

typedef struct {
   int itemSize;
   int count;
   void *list;
} Mystruct;

Mystruct *InitStruct(int itemSize, int count) 
{
  Mystruct *my = malloc(sizeof(Mystruct));
  my->itemSize = itemSize;
  my->count = count;
  //What is the best way to initialize list?  For example:
  //my->list = malloc(count * sizeof(void *));   OR
  //my->list = malloc(count * sizeof(itemSize));
}

//The following should return a pointer to the element stored at a given index 
void *Retrieve(const MyStruct *my, int index)
{
   void *item;
   //What is the best way to return a pointer to the item at the given index from 
   //my->list?
}

Mystruct is similar to an array and void *list is supposed to store the elements or pointers to the elements. Mystruct *InitStruct is a function that initializes a Mystruct pointer and void *Retrieve is a function that returns a pointer to the element stored at a given index.

First, how should I initialize void* list? Should it hold the actual elements or be an array of pointers pointing to the elements?

Second, using the void *Retrieve function, how do I return a pointer to the element stored at a given index in my->list?

  • 1 1 Answer
  • 3 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-14T18:05:22+00:00Added an answer on May 14, 2026 at 6:05 pm

    On the first point, if all elements are the same size, as that itemSize name suggests, then adding one level of indirection (having list point to pointers to items, rather than to items directly) seems to have no added value, so I’d use my->list = malloc(count * itemSize);.

    On the second point, (char*)my->list + itemSize*index should work. You may first want to check that index < my->count, of course (maybe you want to return NULL in that case rather than an invalid pointer).

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

Sidebar

Related Questions

Suppose I have the following code: typedef struct { char **p; } STRUCT; int
Suppose I have the following struct and function returning a pointer: typedef struct {
suppose we have following function: void someFunction(int * araye){ for (int i=0;i<5;i++) cout <<araye[i]<<'
Suppose I have a following linked list structure: struct linked_list { struct linked_list *next;
Suppose I have the following: typedef enum functionType {ln, sin, sqrt} functionType; NSArray *functions
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have the following simple struct: struct Vector3 { double x; double y;
Suppose I have the following list: ID3_tag = ['Title','Artist','Album','Track'] And I have a file
Suppose I have the following method signature int f (int[] values) and I call
Suppose I have following code def foo(x:Int):Unit = { if (x == 1) println

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.