This gives the address of an instance of the structure named one, of type breakfast. I just need to know the name of it - one.
#include <iostream>
struct breakfast
{ int eggs;
int beans;
};
int main()
{
breakfast one;
std::cout << &one;
return 0;
}
Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
you basically can't 'find out the address' of something that does not exist, you need an instance of the given class/struct to take out it's address
when you say, for example
struct MyStruct
{
int someVarName;
};
you don't declare anything(you just Define), thus you can't take it's address unless you do what Moschops showed you.
dospy
Junior Poster in Training
95 posts since Jun 2009
Reputation Points: 65
Solved Threads: 6
If you know nothing else about the structure, it could have a million members or it could have one. Even if you knew the members, you would have to guess based on the compiler and the system architecture and what struct packing options were used and the order the members were declared in the source code.
Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
If it will always be &test.b passed in, you can make a struct of your own inside the function, calculate the difference between the address of your new struct's base address and its b member, and then apply that difference to the value passed in.
Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
that's not the name of the structure, it's an instance of and unnamed structure
basically when you declare a structure without naming it, you are not allowed to create any instances of it, thus the only way to create and instance is by putting names(separated by commas) after the struct declaration AND before the semicolon.
so,
struct{
int a; // note semicolon after declaration, not comma
int b;
char c;
} Check; // note here; it could be Check, Check2, Check3 etc before the semicolon
taking the address is rather simple from now; ex:
#include <iostream>
struct{
int a; // note semicolon after declaration, not comma
int b;
char c;
} Check;
int main( )
{
std::cout << "Address of the unnamed structure instance named 'Check': " << &Check;
return 0;
}
so, if you need to pass the address to the function, just use '&Check';
dospy
Junior Poster in Training
95 posts since Jun 2009
Reputation Points: 65
Solved Threads: 6
that's not the name of the structure, it's an instance of and unnamed structure
basically when you declare a structure without naming it, you are not allowed to create any instances of it, thus the only way to create and instance is by putting names(separated by commas) after the struct declaration AND before the semicolon.
so,
struct{
int a; // note semicolon after declaration, not comma
int b;
char c;
} Check; // note here; it could be Check, Check2, Check3 etc before the semicolon
taking the address is rather simple from now; ex:
#include <iostream>
struct{
int a; // note semicolon after declaration, not comma
int b;
char c;
} Check;
int main( )
{
std::cout << "Address of the unnamed structure instance named 'Check': " << &Check;
return 0;
}
so, if you need to pass the address to the function, just use '&Check';
I just got here and I'm thinking the same thing. I'm glad someone finally noticed.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
yea, the problem is i doubt that's what they wanted, but we can't really help much unless they give us really clean explanation of it
EDIT: now i figured out what they'r problem is, they want to find out the address of a structure by knowing the address of one of it's members
for ex we have
struct
{
int _a; // let's assume that this int's address is 0x02F48C
} Check;
// how do we find out the address of Check without using '&Check'?
i've heard of something like structures have an unusual way to store variables in memory(one right after another, just like an array), but i never really knew it for sure and i haven't even found a tutorial on this(although i'd like to find out about this thing).
sry guys, i can't help you here...
wait for more experienced programmers to answer your questions since it beats me
dospy
Junior Poster in Training
95 posts since Jun 2009
Reputation Points: 65
Solved Threads: 6
You don't need anything more than the offsetof macro in
#include <cstddef>
#include <iostream>
struct A
{
int a ;
int b ;
char c ;
// ...
};
void foo( int* p ) // address of A::b
{
A* pa = reinterpret_cast<A*>( reinterpret_cast<char*>(p) - offsetof(A,b) ) ;
std::cout << "foo::pa: " << pa << '\n' ;
}
int main()
{
A aa ;
std::cout << "&main::aa: " << &aa << '\n' ;
foo( &aa.b) ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
> The thing is that we dont know that the address p is address of b
Then there is no way to reliably determine the address of the struct object.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287