By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

Address of a constant

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
"K. Jennings" <kj*******@resurgence.netwrites:
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.
--
"Programmers 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

P: n/a
"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.securityfocus.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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Chris Dollin <eh@electrichedgehog.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)gmail.com | don't, I need to know. Flames welcome.
May 25 '07 #7

P: n/a
K. Jennings <kj*******@resurgence.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)gmail.com | don't, I need to know. Flames welcome.
May 25 '07 #8

P: n/a

"K. Jennings" <kj*******@resurgence.netha scritto nel messaggio
news:pa*********************@resurgence.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

P: n/a
"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.securityfocus.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

P: n/a
Chris Dollin <eh@electrichedgehog.netwrites:
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.
I think (modulo arguments about the OP's real intent) you can.

It is unclear exactly what would constitute solution (e.g. no
compiler/platform constraints were given and what does "not using an
intermediate variable" really mean, etc) but you *can* do what he/she
*seems* to want to do in standard C:

y = f((int []){123456});

Someone else already pointed this out, but it seems worth another go.

--
Ben.
May 26 '07 #11

P: n/a
"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.
Imagine the consequence of the following:

int f(int *param)
{
return (*param)++ ;
}

Imagine, further, if the compiler combined all such references
to constants, as it can with string literals:

y = f(&123456);
z = f(&123456);

As I recall, FORTRAN passed parameters by reference, and it was
indeed possible to modify "constants" in such a manner.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
May 29 '07 #12

P: n/a
>"K. Jennings" wrote:
>>... Naively, what I would like to do is something like
y = f(&123456) ;
which of course results in a syntax error.
Indeed. As others have noted, C does not have this, but C99 does
have something similar that will work. (Back before there was a
C Standard, VMS C allowed "&constant" in just this way.)

In article <46***************@spamcop.net>
Kenneth Brody <ke******@spamcop.netwrote:
>Imagine the consequence of the following:

int f(int *param)
{
return (*param)++ ;
}

Imagine, further, if the compiler combined all such references
to constants, as it can with string literals:

y = f(&123456);
z = f(&123456);
If the constants are placed in some kind of physically-read-only
memory (write-protected RAM, or ROM, for instance), the attempt
to modify the constant will be trapped or ignored. Otherwise,
"bad things happen". :-)
>As I recall, FORTRAN passed parameters by reference, and it was
indeed possible to modify "constants" in such a manner.
The mechanism in Fortran was not specified, and if a compiler used
value-result, it could ignore attempts to modify constants this way.
Most did tend to use by-value, though, and one could often achieve
"interesting" effects by changing constants.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
May 29 '07 #13

P: n/a
On 29 May 2007 18:47:27 GMT, Chris Torek <no****@torek.netwrote:
"K. Jennings" wrote:
<snip>
As I recall, FORTRAN passed parameters by reference, and it was
indeed possible to modify "constants" in such a manner.

The mechanism in Fortran was not specified, and if a compiler used
Right.
value-result, it could ignore attempts to modify constants this way.
Value-result, in the days before INTENT, _would_ modify the caller's
'constant' just like by-reference, from one call to another call or
use. It would be distinguishable from by-reference only by multiple
references (aliasing) within one call plus its subcalls. And such
problems during one call would at least be near(er) to the cause and
easier to debug; the real bastard is an unintended change caused in
one part of the program that screws up things much later and far away.

By-value (in only) wouldn't, but it wouldn't provide the FORTRAN (pre
INTENT) semantics of allowing changes to any argument.
Most did tend to use by-value, though, and one could often achieve
"interesting" effects by changing constants.
Just for the record, obviously you meant by-reference there.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Jul 1 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.