Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can I get the address of a constant?

Posted on 2003-11-24
3
Medium Priority
?
216 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
[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
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

609 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