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 91k
  • Answers 91k
  • 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
  • Editorial Team
    Editorial Team added an answer You can do it with Flash and either Red5 or… May 11, 2026 at 6:15 pm
  • Editorial Team
    Editorial Team added an answer He needs to do a get latest, try build tree… May 11, 2026 at 6:15 pm
  • Editorial Team
    Editorial Team added an answer Maybe using an iframe could help you out? I think… May 11, 2026 at 6:15 pm

Related Questions

In my experience in object oriented C programming I have seen two ways to
We use a simple object model for our low level networking code at work
I'm writing C cross-platform library but eventually I've got error in my unittests, but
I can't seem to figure out why I am getting an InvalidCastException running the

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.