473,657 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a constant by reference

Hi all,

I want to call a function that takes a void* argument and pass it the address of
a constant. Is there a way to do this without explicitly defining the constant
and then passing the address of the constant? In other words, what I *don't*
want to do is this:

const int myInt = 5;
MyFunc(&myInt);

I want to do something more like this:

MyFunct(&(int)5 );

TIA - Bob
Jun 27 '08 #1
12 1287
Bob Altman wrote:
I want to call a function that takes a void* argument and pass it the address of
a constant. Is there a way to do this without explicitly defining the constant
and then passing the address of the constant? In other words, what I *don't*
want to do is this:

const int myInt = 5;
MyFunc(&myInt);

I want to do something more like this:

MyFunct(&(int)5 );
A constant expression has no address; what you're asking for is meaningless.
It's like asking where the number 5 is stored in the computer. It's not
stored anywhere, it just *is*. Storage locations produce addresses, not
constant values. Aren't you accidentally confusing this with a literal
address, i.e.

MyFunc((void*) 5);

This will convert the integer 5 to a memory location and pass that as a
pointer. This will then most likely produce an access violation upon access,
since "5" is rarely a valid address, but if the "void*" is meant to merely
hold a pointer-sized value rather than an actual pointer, this might have
some meaning.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #2
Bob Altman wrote:
Hi all,

I want to call a function that takes a void* argument and pass it the
address of a constant. Is there a way to do this without explicitly
defining the constant and then passing the address of the constant? In
other words, what I *don't* want to do is this:

const int myInt = 5;
MyFunc(&myInt);
That won't work. Does your function actually accept a "const void *"
instead of a "void *"?
>
I want to do something more like this:

MyFunct(&(int)5 );

TIA - Bob

Jun 27 '08 #3
>Hi all,
>>
I want to call a function that takes a void* argument and pass it the
address of a constant. Is there a way to do this without explicitly
defining the constant and then passing the address of the constant? In other
words, what I *don't* want to do is this:

const int myInt = 5;
MyFunc(&myInt) ;

That won't work. Does your function actually accept a "const void *" instead
of a "void *"?
Yes, my function takes a "const void*"
Jun 27 '08 #4
That won't work. Does your function actually accept a "const void *" instead
of a "void *"?
Actually, it turns out that I left the "const" off of the "void *" in my
function declaration. But I don't see how that affects my original question,
which was how to avoid explicitly declaring the constant and passing its
address.
Jun 27 '08 #5
Bob Altman wrote:
>That won't work. Does your function actually accept a "const void *" instead
of a "void *"?

Actually, it turns out that I left the "const" off of the "void *" in my
function declaration. But I don't see how that affects my original question,
which was how to avoid explicitly declaring the constant and passing its
address.
By taking its address. Let's get a few things straight here. This is a constant:

const int a = 5;

This is *not* a constant, but a constant expression:

5

Expressions (whether constant or not) do not *have* addresses. No storage is
allocated to them. If you want an address, you need a storage location. As I
mentioned, you can do this:

(const void*) 5

This is "the address with numerical value 5". It's not "the address of the
constant 5" because there's no such thing.

Why not take a step back and tell us what you're trying to ultimately
achieve, instead of insisting on the impossible?

--
J.
Jun 27 '08 #6
Bob Altman wrote:
>That won't work. Does your function actually accept a "const void
*" instead of a "void *"?

Actually, it turns out that I left the "const" off of the "void *" in
my function declaration. But I don't see how that affects my
original question, which was how to avoid explicitly declaring the
constant and passing its address.
Once you add the "const" your problem should disappear:

int(5); // creates a temporary of type int
MyFunct(&(int(5 ))); // takes the address of this temporary, references
can be bound to temporaries only if the reference is const
Jun 27 '08 #7
Ben Voigt [C++ MVP] wrote:
Bob Altman wrote:
>>That won't work. Does your function actually accept a "const void
*" instead of a "void *"?
Actually, it turns out that I left the "const" off of the "void *" in
my function declaration. But I don't see how that affects my
original question, which was how to avoid explicitly declaring the
constant and passing its address.

Once you add the "const" your problem should disappear:

int(5); // creates a temporary of type int
MyFunct(&(int(5 ))); // takes the address of this temporary, references
can be bound to temporaries only if the reference is const
This does not compile. We're talking pointers here, not references, and
there's no such thing as a void&.

--
J.
Jun 27 '08 #8
That's just not in the language(s).

Thanks. That's the answer I was afraid I'd get. I'm stuck with passing
pointers 'cause I need to expose stuff using C bindings from a library that's
callable from other languages (Ada, FORTRAN, etc.).
Jun 27 '08 #9
>Once you add the "const" your problem should disappear:
>>
int(5); // creates a temporary of type int
MyFunct(&(int( 5))); // takes the address of this temporary, references
can be bound to temporaries only if the reference is const
This does not compile. We're talking pointers here, not references, and
there's no such thing as a void&.
Here's one that compiles (with VC2008):

void fn1( void * p )
{
int x = *static_cast<in t*>(p);
}

fn1( const_cast<int *>( &static_cast<co nst int &>(5) ) );

.... but I think it stinks! :)

Dave
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2844
by: Johnd | last post by:
I am running into an issue when I try to write more than 1024 characters to a memo field. Apparantly the odbc connection I am using does not permit literals to be larger that 1024 characters. This Memo filed can take virtually infinate data, so the solution seems to be use parameters. My problem is I have never heard of this and need this to work ASAP. Does anyone have a simple example of how Parameters work using ASP?
20
4832
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
5
8992
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the Python code found here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478 //beginning #include <vector>
20
1983
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> bool1=True lst1= def func1(arg1): arg1.append(4)
11
4454
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
2
1313
by: conrad | last post by:
In C, we have pass by value only. This results from values being the product of 'the value of the expression'. Now, I imagine that the C++ standard has the same concept of the 'value of the expression' but I wonder what it yields in such a case when passing a reference to a function that expects a reference. Would it
3
5459
by: Subodh | last post by:
Hi All, In C++ we could pass a constant reference to a function so that the target function could not modify the objects passed eg. const classA & dummmyfunction(const classB) similar thing was valid for objects passed with pointer to constant objects In C++/CLI is there any way for imitating this, I want to pass arguments to and return value from my member function as a constant
20
1990
by: MartinRinehart | last post by:
Is the following correct? x = "some string" x is a reference to "some string" foo(x) Reference is passed to function.
4
2114
by: puzzlecracker | last post by:
How can I pass a reference to a method as constant? I tried the following: Function(const Foo f) or Function(readonly Foo f) Also, How to declare local variable to be constant const Foo foo or readonlyFoo f?
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7316
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.