Solved

want the offset address of a variable

Posted on 2004-09-28
9
177 Views
Last Modified: 2010-04-01
Im new to c++ and I want a function that returns the offset address of a variable.........Im using the API functions.
0
Comment
Question by:ewak
9 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 125 total points
Comment Utility
Use the & operator.

e.g.

  int i = 123;
  cout << "Address of i is " << &i << '\n';
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 125 total points
Comment Utility
To add to above comment:

int * GetOffSet(int& src){
     return &src;
}
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 125 total points
Comment Utility
Could you explain what you mean by the "offset" address?

"Offset" is usually a concept related to the old 16-bit segment model.  

Most of us nowadays use the 32-bit flat model.

One rarely needs the "offset" in this more enlightened era.

Pls explain.

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 86

Assisted Solution

by:jkr
jkr earned 125 total points
Comment Utility
Are you thinking about 'offsetof()'?

#define offsetof(s,m)   (size_t)&(((s *)0)->m)

E.g.

struct tm {
        int tm_sec;     /* seconds after the minute - [0,59] */
        int tm_min;     /* minutes after the hour - [0,59] */
        int tm_hour;    /* hours since midnight - [0,23] */
        int tm_mday;    /* day of the month - [1,31] */
        int tm_mon;     /* months since January - [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday - [0,6] */
        int tm_yday;    /* days since January 1 - [0,365] */
        int tm_isdst;   /* daylight savings time flag */
        };

struct tm mytm;

printf ( "Address offset of 'tm_mon' in 'struct tm' is %d\n", offsetof ( &mytm, tm_day));
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>Any objections should be posted here in the next 4 days. After that time, the question will be closed.

I recommend splitting the points to all four experts {rstaveley, Axter,  grg99, & jkr}

All four experts help give a full answer.
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
Agreed wih Axter
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now