Address of struct and address of first m (2024)

Address of struct and address of first member variable

cl0ckw0rk (1)

The address of a struct is the same as the address of its first member variable.

1
2
3
4
5
6
7
8
9
10
11
 struct myStruct{ int number; char ch;};int main(){ myStruct mystruct = {8,'a'}; cout << &mystruct << endl; cout << &(mystruct.number) << endl;

This prints out the same address. So far so good. Building on this fact I want to access the first member variable using the address of the struct.
Something like this:

cout << *(&mystruct);

I know this is pointless but it is for the sake of learning. Adding the above line makes the code not compile. I tried casting the pointer to an int pointer like so:

cout << (int *)*(&mystruct);

But I still get an error (error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘myStruct’))

Given that both the struct and the first member have the same address, how do we access the member from the struct's address, like I am trying to do above?

And how does the compiler differentiate between doing something with the struct vs doing someting with the first member, if they both share the same address?

seeplus (6563)

Do you mean this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>using namespace std;struct myStruct {int number;char ch;};int main(){myStruct mystruct = {8,'a'};cout << &mystruct << endl;cout << &(mystruct.number) << endl;cout << *(int*)&mystruct << endl;}
000000000019FA80000000000019FA808

But only do things like this for learning purposes!

NB. Read code such as L16 from right to left. Get the address of the struct, cast as a pointer to int (first member), then de-reference this int pointer.

Last edited on

salem c (3697)

Start with

cout << mystruct.number;

Then do

cout << *(&mystruct.number);

It's like the * and the & cancel out, getting you back to where you started.


And how does the compiler differentiate between doing something with the struct vs doing someting with the first member, if they both share the same address?

Because they have different types.
&mystruct is a pointer to the whole structure.
&mystruct.number is a pointer to an integer.

jonnin (11399)

the 'struct' is sort of a 'false entity'. Down in the CPU, there is no such thing as a struct.

maybe this will help...

int i{};
uint8_t bytearray[1000];

int * ip = (int*)bytearray;
i += sizeof(int);
double *dp = (double*)(&bytearray[i]);
i+= sizeof(double);
string *sp = (string*)(&bytearray[i]);
...
this is a 'struct', sort of.
the fields are ip, dp, and sp.

this is (very, very roughly) what the compiler is doing when it turns a 'struct' into machine language, though bytearray would just be ram direct memory offsets.

so you can move the 'struct' around by moving 'bytearray' around as a 'group of bytes', but that is just organization, a 'way to look at it'. there is no 'struct' thing in reality, its just a way to say "this many bytes in a block" when talking at the struct level or "this offset from the starting address" when talking about its fields. That is why the struct and first field share an address: its just a different way of looking at the same block of bytes.

at the c++ level, a struct IS a thing, and a tool you can do a lot with. But that is just a syntax that hides what needs to be done to express it. You do not really care about the memory layout details when using a struct, you just use it and let the compiler do the ugly stuff for you.

Last edited on

JLBorges (13770)

> Given that both the struct and the first member have the same address

This is not guaranteed, except for standard layout types
https://en.cppreference.com/w/cpp/named_req/StandardLayoutType

The object representation of standard layout types are as specified by the C language,
in particular:

A pointer to an object of standard-layout class type can be reinterpret_cast to pointer to its first non-static non-bitfield data member (if it has non-static data members) or otherwise any of its base class subobjects (if it has any) (since C++11), and vice versa. In other words, padding is not allowed before the first data member of a standard-layout type. Note that strict aliasing rules still apply to the result of such cast.
https://en.cppreference.com/w/cpp/language/data_members#Standard_layout

Topic archived. No new replies allowed.

Address of struct and address of first m (2024)
Top Articles
True Detective Season 1 Killer: All The Clues To The Carcosa Reveal
Is W.P. Carey Stock (WPC) a Good Investment?
Bild Poster Ikea
Lexi Vonn
Brady Hughes Justified
Craigslist Vans
Google Sites Classroom 6X
Guardians Of The Galaxy Showtimes Near Athol Cinemas 8
Victoria Secret Comenity Easy Pay
Lichtsignale | Spur H0 | Sortiment | Viessmann Modelltechnik GmbH
Irving Hac
1TamilMV.prof: Exploring the latest in Tamil entertainment - Ninewall
83600 Block Of 11Th Street East Palmdale Ca
World History Kazwire
Https //Advanceautoparts.4Myrebate.com
Sport Clip Hours
California Department of Public Health
3472542504
Binghamton Ny Cars Craigslist
Mary Kay Lipstick Conversion Chart PDF Form - FormsPal
Craigslist Free Stuff Greensboro Nc
Water Days For Modesto Ca
Vigoro Mulch Safe For Dogs
Delaware Skip The Games
Persona 5 Royal Fusion Calculator (Fusion list with guide)
Craigslist Northfield Vt
Greenville Sc Greyhound
Does Hunter Schafer Have A Dick
Vivaciousveteran
Amerisourcebergen Thoughtspot 2023
R Baldurs Gate 3
Free T33N Leaks
Sams Gas Price Sanford Fl
Visit the UK as a Standard Visitor
Mobile crane from the Netherlands, used mobile crane for sale from the Netherlands
Redbox Walmart Near Me
Transformers Movie Wiki
Palmadise Rv Lot
Nsu Occupational Therapy Prerequisites
Tas Restaurant Fall River Ma
Ny Post Front Page Cover Today
Pp503063
Captain Billy's Whiz Bang, Vol 1, No. 11, August, 1920&#10;America's Magazine of Wit, Humor and Filosophy
140000 Kilometers To Miles
F9 2385
All Characters in Omega Strikers
Pike County Buy Sale And Trade
Jigidi Free Jigsaw
Westport gun shops close after confusion over governor's 'essential' business list
Urban Airship Acquires Accengage, Extending Its Worldwide Leadership With Unmatched Presence Across Europe
Tweedehands camper te koop - camper occasion kopen
La Fitness Oxford Valley Class Schedule
Latest Posts
Article information

Author: Nathanial Hackett

Last Updated:

Views: 5573

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Nathanial Hackett

Birthday: 1997-10-09

Address: Apt. 935 264 Abshire Canyon, South Nerissachester, NM 01800

Phone: +9752624861224

Job: Forward Technology Assistant

Hobby: Listening to music, Shopping, Vacation, Baton twirling, Flower arranging, Blacksmithing, Do it yourself

Introduction: My name is Nathanial Hackett, I am a lovely, curious, smiling, lively, thoughtful, courageous, lively person who loves writing and wants to share my knowledge and understanding with you.