Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

want the offset address of a variable

Posted on 2004-09-28
9
Medium Priority
?
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 500 total points
ID: 12168112
Use the & operator.

e.g.

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

Assisted Solution

by:Axter
Axter earned 500 total points
ID: 12168259
To add to above comment:

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

Assisted Solution

by:grg99
grg99 earned 500 total points
ID: 12168526
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 12169657
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
ID: 12661379
>>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
ID: 12662452
Agreed wih Axter
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

721 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