Solved

Can I get the address of a constant?

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

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

717 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