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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:21:24+00:00 2026-05-12T12:21:24+00:00

In C/C++, how do I determine the size of the member variable to a

  • 0

In C/C++, how do I determine the size of the member variable to a structure without needing to define a dummy variable of that structure type? Here’s an example of how to do it wrong, but shows the intent:

typedef struct myStruct {
  int x[10];
  int y;
} myStruct_t;

const size_t sizeof_MyStruct_x = sizeof(myStruct_t.x);  // error

For reference, this should be how to find the size of ‘x’ if you first define a dummy variable:

myStruct_t dummyStructVar;

const size_t sizeof_MyStruct_x = sizeof(dummyStructVar.x);

However, I’m hoping to avoid having to create a dummy variable just to get the size of ‘x’. I think there’s a clever way to recast 0 as a myStruct_t to help find the size of member variable ‘x’, but it’s been long enough that I’ve forgotten the details, and can’t seem to get a good Google search on this. Do you know?

Thanks!

  • 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-12T12:21:25+00:00Added an answer on May 12, 2026 at 12:21 pm

    In C++ (which is what the tags say), your “dummy variable” code can be replaced with:

    sizeof myStruct_t().x;
    

    No myStruct_t object will be created: the compiler only works out the static type of sizeof’s operand, it doesn’t execute the expression.

    This works in C, and in C++ is better because it also works for classes without an accessible no-args constructor:

    sizeof ((myStruct_t *)0)->x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to determine the size of a UITableCellView. The reason being that
I am curious whether it is possible to determine the maximum size that an
How can I define an array as a member of class while its size
How can I best determine the Size of a Class from Memory? Here is
I'm looking a way to determine size in bytes of a value type (TypeReference)
Is there a query or function that I can use to determine the size
I used code like #define BLOCK_SIZE 32 to determine block size for kernel function.
The documentation says that one should not use available() method to determine the size
Using Javascript, is there a way that I can determine the size of a
Out of interest, is it possible to determine the size of a JavaScript variable,

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.