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

Home Posts Topics Members FAQ

Address of a constant

I have a function f with the following prototype: int f(void *).
From my main() (or whatever) I can do things like

int x = 123456 ;
int y ;

y = f(&x) ;

which is fine. My question is, how can I pass the 123456 integer to f
without using the intermediate x variable? Naively, what I would like to
do is something like

y = f(&123456) ;

which of course results in a syntax errors.

May 25 '07 #1
13 1640
"K. Jennings" <kj*******@resu rgence.netwrite s:
I have a function f with the following prototype: int f(void *).
From my main() (or whatever) I can do things like

int x = 123456 ;
Note that 123456 is outside the portable range of int.
int y ;

y = f(&x) ;

which is fine. My question is, how can I pass the 123456 integer to f
without using the intermediate x variable? Naively, what I would like to
do is something like

y = f(&123456) ;

which of course results in a syntax errors.
In C99, you can use a compound literal, e.g.
y = f(&(int) {123456});
or
y = f((int[]) {123456});
(I'm no C99 expert so it's possible that one or both of these is
wrong for some reason.)

I don't think there's a way to do this in C89.
--
"Programmer s have the right to be ignorant of many details of your code
and still make reasonable changes."
--Kernighan and Plauger, _Software Tools_
May 25 '07 #2
"K. Jennings" wrote:
>
I have a function f with the following prototype: int f(void *).
From my main() (or whatever) I can do things like

int x = 123456 ;
int y ;

y = f(&x) ;

which is fine. My question is, how can I pass the 123456 integer
to f without using the intermediate x variable? Naively, what I
would like to do is something like

y = f(&123456) ;

which of course results in a syntax errors.
Why not "int f(int);", which is more direct and safer.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 25 '07 #3
On Fri, 25 May 2007 12:13:06 -0400, CBFalconer wrote:
"K. Jennings" wrote:
>>
I have a function f with the following prototype: int f(void *). From
my main() (or whatever) I can do things like

int x = 123456 ;
int y ;

y = f(&x) ;

which is fine. My question is, how can I pass the 123456 integer to f
without using the intermediate x variable? Naively, what I would like
to do is something like

y = f(&123456) ;

which of course results in a syntax errors.

Why not "int f(int);", which is more direct and safer.
Because in the case that I am interested in the data buffer fed
to f can contain lots of different data types, that will be dealt with
appropriately in the right contexts.
May 25 '07 #4
K. Jennings wrote:
I have a function f with the following prototype: int f(void *).
From my main() (or whatever) I can do things like

int x = 123456 ;
int y ;

y = f(&x) ;

which is fine. My question is, how can I pass the 123456 integer to f
without using the intermediate x variable?
You can't.
Naively, what I would like to
do is something like

y = f(&123456) ;

which of course results in a syntax errors.
Even if it worked, what's `f` going to do with it? It's
handed a pointer-to-object, but it doesn't know what
kind of object. It can pass it along to another
function that wants a `void*`, but the same applies
to /that/. It can store it in a `void*` variable,
but if that's to be pointful, someone somewhere has
to know what to do with it. It could `free` or `realloc`
it, but since your examples don't pass mallocated
store, that would be ... unwise.

Are you /sure/ that prototype is the one you want?

--
Untyped Hedgehog
The shortcuts are all full of people using them.

May 25 '07 #5
Chris Dollin said:
K. Jennings wrote:
>I have a function f with the following prototype: int f(void *).
From my main() (or whatever) I can do things like

int x = 123456 ;
int y ;

y = f(&x) ;

which is fine. My question is, how can I pass the 123456 integer to f
without using the intermediate x variable?

You can't.
Yes, he can, by changing the prototype of f so that it expects int
rather than int *. Then he can pass the integer value like this:

y = f(123456);

provided, of course, that INT_MAX >= 123456 on his system.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 25 '07 #6
Chris Dollin <eh@electriched gehog.netwrote:
K. Jennings wrote:
y = f(&123456) ;
which of course results in a syntax errors.
Even if it worked, what's `f` going to do with it?
The same thing f would do with a pointer to an automatic int,
presumably. Obviously f() is intended to have some way of knowing
what the type of its argument is (as OP explicitly stated elsethread).

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
May 25 '07 #7
K. Jennings <kj*******@resu rgence.netwrote :
int x = 123456 ;
int y ;
y = f(&x) ;
My question is, how can I pass the 123456 integer to f
without using the intermediate x variable? Naively, what I would like to
do is something like
y = f(&123456) ;
which of course results in a syntax errors.
As has been stated, you can't do this. It's worth thinking about the
question of "Why not?", which is (*) that it would require such
integer constants to have storage associated with them, much like
string literals have associated storage. (Note that you can (**)
compute the address of a string literal; the resulting pointer has
type "pointer to array of char".)

(*) - Add grain of salt here; I am not a guru.

(**) - Assuming my informal test with gcc and my reading of the
standard are correct.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
May 25 '07 #8

"K. Jennings" <kj*******@resu rgence.netha scritto nel messaggio
news:pa******** *************@r esurgence.net.. .
I have a function f with the following prototype: int f(void *).
From my main() (or whatever) I can do things like

int x = 123456 ;
int y ;

y = f(&x) ;

which is fine. My question is, how can I pass the 123456 integer to f
without using the intermediate x variable? Naively, what I would like to
do is something like

y = f(&123456) ;

which of course results in a syntax errors.
What are you trying to do, considering that the argument of f isn't even
const?
Supposing f is
int f(void *v)
{
return ++*(int *)v;
}
What do you expect to happen? All next occurrences of 123456 to behave as
if they were 123457?
More seriously, if f is actually a int f(const void*, const void*) you
use for qsort, and you want to use it to compare two int constants
without rewriting it, you may use temporary variables:

int flag;
{
tmp1 = 123456;
tmp2 = 42;
flag = f(&tmp1, &tmp2);
}

But you'd better tell *what* you are trying to do, than telling *how* you
want to do that, which could be the wrong way.
May 25 '07 #9
"K. Jennings" wrote:
CBFalconer wrote:
>"K. Jennings" wrote:
>>>
I have a function f with the following prototype: int f(void *).
From my main() (or whatever) I can do things like

int x = 123456 ;
int y ;

y = f(&x) ;

which is fine. My question is, how can I pass the 123456 integer
to f without using the intermediate x variable? Naively, what I
would like to do is something like

y = f(&123456) ;

which of course results in a syntax errors.

Why not "int f(int);", which is more direct and safer.

Because in the case that I am interested in the data buffer fed to
f can contain lots of different data types, that will be dealt with
appropriately in the right contexts.
Then you better redefine f so that it knows the type involved, or
define a carrying struct which imparts that info. You will have to
use a void*, since you can't convert pointer types arbitrarily.
But a void* can carry any data pointers information, and can be
auto-converted back to the original type.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 25 '07 #10

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

Similar topics

7
2149
by: Nolan Martin | last post by:
is a static functions address constant? ie.. static void func(); write_to_file(&func); Restart program... static void func(); void (*funcPtr) ();
9
3237
by: cppsks | last post by:
Taking the address of a static const resulted in a unresolved symbol. Why is that? Is the address assigned at load time? Thanks.
35
10771
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
15
2232
by: dandelion | last post by:
Hi, Just another question for the standards jockeys... Suppose I have an Interrupt Vector Table located at address 0x0000 (16-bit machine). I want to dump the context of the IVT, by treating it as an array starting at (you guessed it) 0x0000. So I would have struct iv_s* ivt = (struct iv_s *) 0x0000;
33
3154
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
21
6999
by: srikar | last post by:
hi all when I am running the below program #include<iostream> enum one { a=1000,b=2000,c,d,z}; int main() { one* a1;one* a2;one* a3;
3
2334
by: Old Wolf | last post by:
The code is: extern int x; char *ptr1 = 8 + (char *)&x; char *ptr2 = (char *)(8 + (unsigned)&x); My understanding is that the ptr1 declaration is correct but the ptr2 is not, and the compiler can reject it (even if the compiler has a meaningful definition of integer<->pointer conversions).
17
2312
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of the notion of
7
2806
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not naming names here to remove bias - I'm trying to tell if I'm relying on implementation defined behavior or if this is a bug in the new compiler. Consider this stripped down example:
7
9630
by: Guillaume Dargaud | last post by:
Hello all, I have an example of working code under my eyes that goes as follow: unsigned long address=0x400000; (void (*)(void)address)(); It's supposed to jump start a kernel loaded at that address from a small bootloader. But my cross compiler chokes on the second line (89) and I must say I've
0
8392
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
8305
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
8726
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
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
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
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
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.