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. 13 1624
"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_
"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
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.
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.
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.
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.
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.
"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.
"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
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.
"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>
>"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 (40°39.22'N, 111°50.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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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) ();
|
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.
|
by: hasho |
last post by:
Why is "call by address" faster than "call by value"?
|
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...
|
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);
|
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;
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |