Solved

Can I get the address of a constant?

Posted on 2003-11-24
3
212 Views
Last Modified: 2010-04-02
Hi!

I have something like this:

#define MYCONST 666

Since I want the constant to become a key in a mapping class that expects void-pointers I thought I could hand over the address of MYCONST, but

&MYCONST

produces a compiler error.

Is there any way to reference the address of MYCONST in C++?

0
Comment
Question by:hirnsieb
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 9811062
>>Is there any way to reference the address of MYCONST in C++?

Not in the way you are using it - the above is a preprocessor macro that tells the compiler to replace every occurance of MYCONST with 666. It would work if you'd use

const MYCONST = 666;
0
 
LVL 13

Expert Comment

by:SteH
ID: 9811070
#define is something for the preprocessor. It will be translated before the compiler works to:
&666. This is not a legal C/C++ statement.


Declare it as a constant not as a preprocessor macro:

const int MYCONST = 666;

now &MYCONST is legal.
0
 
LVL 3

Expert Comment

by:RJSoft
ID: 9811240
Or

int *Myconst = MYCONST;

printf(" address is %x", Myconst);

RJ
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  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 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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 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