Solved

Can I get the address of a constant?

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

759 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

24 Experts available now in Live!

Get 1:1 Help Now