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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:47:53+00:00 2026-05-11T10:47:53+00:00

I have a testing struct definition as follows: struct test{ int a, b, c;

  • 0

I have a testing struct definition as follows:

struct test{     int a, b, c;     bool d, e;     int f;     long g, h; }; 

And somewhere I use it this way:

test* t = new test;   // create the testing struct int* ptr = (int*) t; ptr[2] = 15;          // directly manipulate the third word cout << t->c;         // look if it really affected the third integer 

This works correctly on my Windows – it prints 15 as expected, but is it safe? Can I be really sure the variable is on the spot in memory I want it to be – expecially in case of such combined structs (for example f is on my compiler the fifth word, but it is a sixth variable)?

If not, is there any other way to manipulate struct members directly without actually having struct->member construct in the code?

  • 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. 2026-05-11T10:47:54+00:00Added an answer on May 11, 2026 at 10:47 am

    It looks like you are asking two questions

    Is it safe to treat &test as a 3 length int arrray?

    It’s probably best to avoid this. This may be a defined action in the C++ standard but even if it is, it’s unlikely that everyone you work with will understand what you are doing here. I believe this is not supported if you read the standard because of the potential to pad structs but I am not sure.

    Is there a better way to access a member without it’s name?

    Yes. Try using the offsetof macro/operator. This will provide the memory offset of a particular member within a structure and will allow you to correctly position a point to that member.

    size_t offset = offsetof(mystruct,c); int* pointerToC = (int*)((char*)&someTest + offset); 

    Another way though would be to just take the address of c directly

    int* pointerToC = &(someTest->c); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Personally, I would simply pass in the book as a… May 11, 2026 at 12:03 pm
  • added an answer In the PreInit function place: this.MasterPageFile = '~/masterPage.master'; Yes, you… May 11, 2026 at 12:03 pm
  • added an answer You should use stored procedures wherever it is possible. That… May 11, 2026 at 12:03 pm

Related Questions

Setup I have a VBScript for driving the stress testing of a web service.
I have a simple website I'm testing. It's running on localhost and I can
I have done a bit of testing on this myself (During the server side
I have a class that I am unit testing into with DUnit. It has
I have a project that I would like to start beta testing soon, it
I have a php server that is running my domain name. For testing purposes
I have a form that has multiple fields, and for testing purposes is there
I am starting out with unit testing, I have a method that uses the
Using Visual Studio 2008 / C# / VS Unit Testing. I have a very
I have been using CPPUnit as a unit testing framework and am now trying

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.