Solved

Can I get the address of a constant?

Posted on 2003-11-24
3
210 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

803 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