Hi all,
How to find the base address of a structure ,just by knowing the name of the structure and a address of one of its members.

Thanks in advance

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;
}

The instance name is not known!!!! Only the name of the struct(breakfasr) and address of one of its members say &one.eggs is known.Using these two data the base address needs to be found out!!!

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.

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.

This is the xact problem given to me:

Struct{

  Int a,

 Int b,

 char c,

} Check;

 

Void Func(int *ptr)

{

 /* How will you get the base address of the structure*/

 You know only the name of the structure

}

 

 

Main()

{

 Check test;

Test.b = 0x01;

Func(&Test.b)

}

QUESTION:

Passing an element of a structure from a function to another function how will you get the base address of the structure itself.

You Know:

1) Only the name of the structure

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.

No,any address of members can be passed in.We dont know that the address is of b.

Thanks for ur reps!!!

Can any1 find a soluution to this problem!!!!

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';

commented: Someone finally noticed. +13

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.

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

Ya ur rite!!! i know the address of one of its members and a structure name say for ex.

struct cook
{
int brad;
char egg;
}eat;

i will pass the address &eat.egg to a function , knowing this address and the structure name i.e cook ,i need to find the base address of the structure in that function

Waiting for d reps.
Thanks in advance

There is no solution with just functions. You need a macro, very similar in fact to offsetof.

do we have nay such macro like offsetof . In offsetof we give the structure name and the member name,am asking for a macro which gets the structure name and the address of a member.or atlest is it possible to write a code for that.

:)

You don't need anything more than the offsetof macro in <cstdddef>

#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) ;
}

If you know the name of the *member* of the structure and you know the type of the structure, you can use offsetof(type,member) and do some pointer arithmetic.

It is impossible to get the base of the structure in C (or C++) without knowing what member it is and what type it is.

You must be mistaken that that is the "exact" problem given to you. The structure is unnamed, and you don't know what member *ptr is pointing to.

Please double check that you have posted the exact problem. The answer to the current assignment given is, "it's impossible".

MAYBE you can cheat and use decltype(Check) to get the type of the unnamed type (never tried to do something silly like that, not sure it works). Then if you miraculously knew that it was member 'b' in Func, then you could use:

void Func(void *p)
{
    typedef decltype(Check) StructType;
    size_t memberofs = offsetof(StructType,b);
    void *base = (void*)((char*)p - memberofs);
    cout << "The base address is " << base;
}

If you're cheating that much you might as well just say &Check is the base address.

:)

The thing is that we dont know that the address p is address of b,then how can we put the member name b in offsetof() i.e inside the function i know only the name of the structure (A ,in this case) and address *p.

> 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.

Exactly, which is why I said "if you miraculously knew that it was member 'b' in Func"...

The problem given is impossible because:
1) The struct is unnamed
2) We can't assume that it's the Check instance of the structure. If we could then we can ignore *p and just say &Check is the base!
3) From a raw address it is impossible to determine a struct offset without knowing what member it is.
4) Without an offset, calculating the base is impossible.

Double check that you have posted the actual problem correctly.

ohk i ve edited the code now.

Struct check{
     
    Int a,
     
    Int b,
     
    char c,
     
    } test;
     
     
     
    Void Func(int *ptr)
     
    {
     
    /* How will you get the base address of the structure*/
     
    You know only the name of the structure
     
    }
     
     
     
     
     
    Main()
     
    {
     
    
     
    Test.b = 0x01;
     
    Func(&Test.b)
     
    }

The problem is quite clear....I need to find the base address of the struct .I know that the name is Check and the address of one of the member is p

>>The problem is quite clear....I need to find the base address of the struct .I know that the name is Check and the address of one of the member is p

And the answer is clear: IT IS IMPOSSIBLE!

But it seems a solution must be available.That's why this problem has been asked to me?!!!
:P

it seems that you misunderstood what your teacher asked you to do..

But what all can we do using a structure name and the address of one of its members??!! Is it impossible to find the base address using two of these info??

>>Is it impossible to find the base address using two of these info??

"Insanity: doing the same thing over and over again and expecting different results." -Albert Einstein

How many times will you ask the same question and expect a different answer?

The answer is clear, to find the base address of the struct, you need to know which member that pointer points to, if you don't know which member it is, only its address, then it is IMPOSSIBLE to find the base address of the struct.

Ohk COOL B-)

Ohk!!!!!!!!! Ill try posting the answer once i get it !!!
Thanks guys for the reps!!!!
If i come to know that the question is a wrong one , am SORRY!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.