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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:48:04+00:00 2026-05-21T17:48:04+00:00

I have a simple struct: typedef struct { void *things; int sizeOfThings; } Demo;

  • 0

I have a simple struct:

typedef struct {
    void *things;
    int sizeOfThings;
} Demo;

things is intended to contain an array of individual “thing”, like maybe strings or ints.
I create a pointer to it:

Demo * Create(int value) {
    Demo *d = malloc(sizeof(Demo));
    if (d != NULL) {
        d->sizeOfThings = value;
        d->things = malloc(20 * value); // We'll have a max of 20 things
    }
}

value is sizeof(int) for an array of ints, for example.

If in another function I want to insert something into d->things (assuming at least for not that I’m just adding it to the first slot, position management done elsewhere):

char * thing = "Me!";
strncpy(d->things[0], &thing, d->sizeOfThings);

I get around the strncpy area

test.c:10: warning: pointer of type ‘void *’ used in arithmetic
test.c:10: warning: dereferencing ‘void *’ pointer
test.c:10: error: invalid use of void expression

I’m just trying to understand the use of void* as a way to generalize my functions. I suspect there’s something wrong with d->things[0].

  • 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-21T17:48:04+00:00Added an answer on May 21, 2026 at 5:48 pm

    According to the C standard, void has no size– sizeof(void) is undefined. (Some implementations make it sizeof(int) but this is non-compliant.)

    When you have an array of type foo, this expression:

    array[3]
    

    Adds 3*sizeof(foo) to the address stored in array and then deferences that. That’s because the values are all packed together in memory. Since sizeof(void) is undefined, you can’t do that for void arrays (in fact you can’t even have void arrays, only void pointers.)

    You must cast any void pointer to another pointer type before treating it as an array:

    d->things = malloc(20 * sizeof(int));
    (int *)(d->things)[0] = 12;
    

    However, keep in mind that you don’t even have to do that to use strncpy on it. Strncpy can accept a void pointer just fine. But you were using strncpy incorrectly. Your strncpy invocation should look like this:

    strncpy(d->things, thing, d->sizeOfThings);
    

    What your version would have done was try to treat the first array member of d->things as a pointer when it’s not, and would have treated &thing, which is a char **, as if it were just a char *.

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

Sidebar

Related Questions

You have a simple struct, say: struct rect { int x; int y; int
So I have a structure of unknown size as follows: typedef struct a{ int
I have a fairly simple const struct in some C code that simply holds
I have a simple question in understanding the pointers and struct definitions in the
i'm trying to implement a simple array of function descriptors of type fun_desc struct
I have this structure which I want to write to a file: typedef struct
I have a class with a few simple structs like this (simplified code) inside
I have simple regex \.*\ for me its says select everything between and ,
Ok, i have simple scenario: have two pages: login and welcome pages. im using
In general, is it a best practice to have simple POJO Java classes implement

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.